Skip to content

Commit

Permalink
Add support for dynamic import() syntax in TS types (#380)
Browse files Browse the repository at this point in the history
Turns out this didn't work before, and I tried to use it when setting up code
splitting in the playground. This is mostly based on `parseImportType` from
the TypeScript codebase.
  • Loading branch information
alangpierce committed Dec 29, 2018
1 parent 16a44ab commit 08186f0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/parser/plugins/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,24 @@ function tsParseThisTypeNode(): void {

function tsParseTypeQuery(): void {
expect(tt._typeof);
tsParseEntityName();
if (match(tt._import)) {
tsParseImportType();
} else {
tsParseEntityName();
}
}

function tsParseImportType(): void {
expect(tt._import);
expect(tt.parenL);
expect(tt.string);
expect(tt.parenR);
if (eat(tt.dot)) {
tsParseEntityName();
}
if (match(tt.lessThan)) {
tsParseTypeArguments();
}
}

function tsParseTypeParameter(): void {
Expand Down Expand Up @@ -411,6 +428,9 @@ function tsParseNonArrayType(): void {
case tt._typeof:
tsParseTypeQuery();
return;
case tt._import:
tsParseImportType();
return;
case tt.braceL:
if (tsLookaheadIsStartOfMappedType()) {
tsParseMappedType();
Expand Down
17 changes: 17 additions & 0 deletions test/typescript-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,23 @@ describe("typescript transform", () => {
let T = 3, b = 4, c = 5, d = 6;
console.log(T, b, c, d);
}
`,
);
});

it("handles import() types", () => {
assertTypeScriptESMResult(
`
type T1 = import("./foo");
type T2 = typeof import("./bar");
type T3 = import("./bar").Point;
type T4 = import("./utils").HashTable<number>;
`,
`
`,
);
});
Expand Down

0 comments on commit 08186f0

Please sign in to comment.