Issue
Let's say I have two files, A.js and B.js. Both need references to each other like this.
A.js
import { B } from "b"
export class A {
constructor(public name: string) {}
}
let b = new B();
b.print(new A("This is a random name"));
B.js
import { A } from "a"
export class B {
print(a: A) {
console.log(a.name);
}
}
The example above will create a circular reference which currently does not work in the JavaScript runtime I'm using. The file B.js really only need the type information, not the actual export object). I want the type from A.js to get static type checking. Is this possible?
Solution
NOTE: This answer is outdated. The "import elision" feature described here was removed in TypeScript 4.9. Other, newer answers show better ways of doing type-only imports like import type {A} from 'a'
or type A = import('a').A
You don't need to do anything special to import only type information from module a
.
> The compiler detects whether each module is used in the emitted
> JavaScript. If a module identifier is only ever used as part of a type
> annotations and never as an expression, then no require call is
> emitted for that module.
Most likely, the example code you posted in your question is incomplete, and real b
module has runtime dependency on a
- find out where it is and get rid of that, and you won't have this problem.
Answered By - artem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.