-
Hi to all. I am using node 23.7 and experiment with Running TypeScript Natively. The docs say that we must using import types like this: import type { Type1, Type2 } from './module.ts'; I understand what "import type" does but the problem here is that I import a type from another file so to create objects from it and with 'import type" I cannot. So, what is the correct way to do that (import types so to create objects from them) and also being able to run TS natively in node? Thank you in advance for any help on this! |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 4 replies
-
Types are in type space; object are in value space - you can't create an object from a type afaik. Can you elaborate? |
Beta Was this translation helpful? Give feedback.
-
Hey ljharb, thanks for the help! I have the Token.ts (with the exported class Token) and the Scanner.ts files. In Scanner.ts I import the Token type from Token.ts and I can create an object from it: import {Token} from "./Token"; When I use the traditional way (i.e. compiling the .ts files through tsc to produce the JS files), everything is working correctly. But when I try to run typeScript natively, I have to use import type {Token} from "./Token.ts" and of course I cannot create the Token object any more. I am sure I am missing something here... |
Beta Was this translation helpful? Give feedback.
-
The above 2 lines works as expected when I use TS to generate the JS files (and running the JS files through node). But if I do this (because it is required by node to run TS natively): import type {Token} from "./Token.ts"; then TS tells me that: "TS1361: cannot be used as a value because it was imported using import type" and node: "ReferenceError: Token is not defined". I want to find a way to bee able to create token objects and run the program "TS natively" in node now that it allows it. |
Beta Was this translation helpful? Give feedback.
-
Sorry, I got lost :) I use import type {Token} from "./Token.ts"; so the Token class becomes available in the Scanner.ts file to use it there. Is there another way to do this? What do you mean "leave it as just import"? Again, thank you for helping me out here! |
Beta Was this translation helpful? Give feedback.
-
Ok, now I understand that import type/value distinction that you told me... Ok, I did what you said: import {Token} from "./Token.ts" and now it worked (to run the TS natively in node) but I had to put these statements in my tsconfig file first:
So, "import type" can be used to import "type" and "interface" types and "import" for class values so to be able to run TS natively in node, correct? |
Beta Was this translation helpful? Give feedback.
-
Cannot thank you enough!! I will start using only the TS-native way and not the "transpire to JS" one to see how far I/it can go. Thank you again!! |
Beta Was this translation helpful? Give feedback.
import { Token } from './Token.ts'
.import type
is only used when you just want a type, and if you're trying tonew
the class, you don't just want a type.