Skip to content

TypeScript type imports #206

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jnordberg opened this issue Jul 23, 2020 · 9 comments
Closed

TypeScript type imports #206

jnordberg opened this issue Jul 23, 2020 · 9 comments
Labels
invalid This doesn't seem right question Further information is requested

Comments

@jnordberg
Copy link

Currently you cannot import TypeScript types using standard import {MyInterface} from 'mylib' syntax but must use import type {MyInterface} from 'mylib' instead. This is a bit inconvenient and non-obvious since Vue and many others allow using the standard syntax, is this something that could be improved or is it a limitation of svelte?

Refs sveltejs/template#146

@kaisermann
Copy link
Member

kaisermann commented Jul 23, 2020

This is a known limitation. import type is needed because we need to remove type imports before passing it to svelte/the bundler. Since we don't do type-checking anymore, it's not possible to know what kind of import we're dealing with (see it here).

edit: Just added a quick link regarding the limitation in the docs: https://github.com/sveltejs/svelte-preprocess/blob/master/docs/preprocessing.md#typescript---limitations

@kaisermann kaisermann added invalid This doesn't seem right question Further information is requested labels Jul 23, 2020
@orta
Copy link

orta commented Jul 23, 2020

I can fix this at TypeScript level for Svelte in the tsconfig via https://www.staging-typescript.org/tsconfig#importsNotUsedAsValues

@kaisermann
Copy link
Member

@orta Didn't know about that prop! I don't have the time to test it right now, but does it work when transpileOnly is true? If yes, would probably make sense to force it from our side here

@orta
Copy link

orta commented Jul 23, 2020

I think it's worth turning on there, but I'm not 100% if it runs in transpileOnly because it won't have a type system to back up its interpretation (so it's fine either way)

@dominic-simplan
Copy link

This change seems to break my code which was working fine with version 1.0.2.
I am importing typescript types from other files as in import { GraphData } from "./GraphData";. This was working fine previously and now I am getting the error:
This import is never used as a value and must use 'import type' because the 'importsNotUsedAsValues' is set to 'error'.

If I understand you correctly, my code wasn't supposed to work actually? Or was it working because my typescript files only contain code which doesn't need to be preprocessed (e.g. no decorators, ...)?

@orta
Copy link

orta commented Jul 24, 2020

Your code used to work kinda by accident is mainly the point, the identifier GraphData was included in your JS at runtime but it wasn't being used and most of the time that can be OK but sometimes that can overwrite the wrong thing and you would be hard pressed to know why that broke

@johnpapa
Copy link

johnpapa commented Oct 1, 2020

Thanks for posting this. I also ran into this and didn;t want to add import type ... to all of my code. So I added the setting to my tsconfig.json

{
  "extends": "@tsconfig/svelte/tsconfig.json",

  "include": ["src/**/*"],
  "exclude": ["node_modules/*", "__sapper__/*", "public/*"],
  "compilerOptions": {
    "importsNotUsedAsValues": "remove"
  }
}

moufmouf added a commit to workadventure/workadventure that referenced this issue May 12, 2021
Turning the "importsNotUsedAsValues" TS config value to "error".
This will require us to use `import type` instead of `import` when we are importing a value that is only used as a type (and therefore that is dropped by the Typescript compiler).

Why this change?
This is a requirement to be able to use Svelte in the future. See sveltejs/svelte-preprocess#206 (comment)
moufmouf added a commit to workadventure/workadventure that referenced this issue May 12, 2021
Turning the "importsNotUsedAsValues" TS config value to "error".
This will require us to use `import type` instead of `import` when we are importing a value that is only used as a type (and therefore that is dropped by the Typescript compiler).

Why this change?
This is a requirement to be able to use Svelte in the future. See sveltejs/svelte-preprocess#206 (comment)
@derolf
Copy link

derolf commented Aug 19, 2021

I am quite often getting the error The requested module '...' does not provide an export named '...'. Usually, the fix is to us import type at the importing module. However, in my tsconfig, importsNotUsedAsValues is set to error. So, I am confused why the preprocessor isn't properly catching the problem...

@kpietraszko
Copy link

kpietraszko commented Jun 4, 2023

For some reason this was removed from the Typescript Limitations section, while this still is an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right question Further information is requested
Projects
None yet
Development

No branches or pull requests

7 participants