Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error importing type definitions? #169

Closed
kernel-io opened this issue Dec 11, 2018 · 9 comments
Closed

Error importing type definitions? #169

kernel-io opened this issue Dec 11, 2018 · 9 comments

Comments

@kernel-io
Copy link

kernel-io commented Dec 11, 2018

If you find you are getting an error while trying to import type definitions, make sure to add the name of the package with type definitions to the excludes array in your autoImport configuration in ember-cli-build.js 馃憤

@kernel-io kernel-io changed the title How to import Type definitions Error importing type definitions Dec 11, 2018
@kernel-io kernel-io changed the title Error importing type definitions Error importing type definitions? Dec 11, 2018
@ef4
Copy link
Collaborator

ef4 commented Dec 11, 2018

Thanks for reporting and for suggesting a workaround. I think we can fix this so it does the right thing automatically.

Option 1: When we can't resolve a module, before giving up and generating an error we could try to resolve a type definition, and if we find one we would ignore the import (as it doesn't imply any runtime code is needed).

Option 2: If we switch to using a babel plugin for analysis (which has been discussed before), and if we can control the relative order of our plugin and the one ember-cli-typescript installs, we can do analysis after TS has already compiled away the type-only imports.

@kernel-io
Copy link
Author

I'm not educated regarding the benefits of using the babel plugin for analysis, however if using it brings more benefits / opens up more avenues than option 1, wouldn't it be better to follow that path? Option 1 seems very very specific to this issue only?

@tschoartschi
Copy link

tschoartschi commented Apr 3, 2019

We actually hit the same problem... But we can not work around this issue because the package has typings and source in the same package. What we would need is:

autoImport: {
    exclude: ['web-sdk/lib/definitions']
}
import WebSdk from 'web-sdk'; // behind this real JS code sits
import {GlbViewer} from 'web-sdk/lib/definitions/glb-viewer-core/src/glb-viewer'; // this is only type information

export const GLB_CANVAS_ID = 'glb-canvas-container';

export default class GLBViewer extends Component {
    private _glbViewer: GlbViewer;

    constructor() {
    	this._glbViewer = await WebSdk.getGlbViewer();
    }
}    

Since the package web-sdk is a package we control we could adjust it, if it makes sense but many customers use it already so we need a good reason to change it. There where no problems with any of the other major frameworks (Vue, React, Angular, custom Rollup build).

Would be great if we could provide a workaround for Ember users

@tschoartschi
Copy link

tschoartschi commented Apr 3, 2019

@dfreeman showed us a workaround in discord:

export default class GLBViewer extends Component {
    private _glbViewer: import('web-sdk/lib/definitions/glb-viewer-core/src/glb-viewer').GlbViewer;
}

But that's not very nice, it looks awkward and furthermore the IDE always suggests the former import style. So we always have to manually adjust

But the good thing is, we are not blocked with this workaround anymore 馃槂

@ef4
Copy link
Collaborator

ef4 commented Apr 10, 2019

There is another case that I think is worth including in this issue. When importing something only for use as a type, you really don't want that to create a real runtime dependency on the module. That interferes with your ability to lazily load the module.

Moving our analysis into a babel plugin isn't even a slam dunk, because there would still be finicky timing concerns there. Changes to the implementations of other babel plugins could break the analysis.

So I'm starting to think we need to introduce our own detection of type-only imports.

@boris-petrov
Copy link
Contributor

Is this bug going to be circumvented when using the soon-to-be-available import type functionality of TypeScript?

@ef4
Copy link
Collaborator

ef4 commented Feb 6, 2020

Yes, that should work, though I still think we should fix the other case too.

@boris-petrov
Copy link
Contributor

boris-petrov commented Mar 24, 2020

@ef4 - just to let you know that the same problem exists even with type-only imports unfortunately. Is that something that is more easily fixable than the original issue? At least to have some way of doing this.

P.S. Well, the workaround from above still works, so it's not actually that big of a deal. But just to inform you that type-only imports from TypeScript 3.8 and Babel 7.9 have the same problem.

@mansona
Copy link
Member

mansona commented Aug 9, 2023

Has anyone tried this in Typescript 4.x or 5.x? I'm curious if the type-only import in newer Typescript might have fixed this 馃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants