Skip to content

Commit cb32000

Browse files
committed
feat(type-compiler): emit typeName for type only imports
1 parent 455f243 commit cb32000

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

packages/type-compiler/src/compiler.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ export class ReflectionTransformer implements CustomTransformer {
13141314
}
13151315
}
13161316

1317-
if (narrowed.name) program.pushOp(ReflectionOp.typeName, program.findOrAddStackEntry(getIdentifierName(narrowed.name)));
1317+
if (narrowed.name) this.resolveTypeName(getIdentifierName(narrowed.name), program);
13181318

13191319
const description = extractJSDocAttribute(narrowed, 'description');
13201320
if (description) program.pushOp(ReflectionOp.description, program.findOrAddStackEntry(description));
@@ -2125,7 +2125,7 @@ export class ReflectionTransformer implements CustomTransformer {
21252125
if (resolved.importDeclaration) {
21262126
//if explicit `import {type T}`, we do not emit an import and instead push any
21272127
if (resolved.typeOnly) {
2128-
program.pushOp(ReflectionOp.any);
2128+
this.resolveTypeOnlyImport(typeName, program);
21292129
return;
21302130
}
21312131

@@ -2206,7 +2206,7 @@ export class ReflectionTransformer implements CustomTransformer {
22062206
} else if (isClassDeclaration(declaration) || isFunctionDeclaration(declaration) || isFunctionExpression(declaration) || isArrowFunction(declaration)) {
22072207
//if explicit `import {type T}`, we do not emit an import and instead push any
22082208
if (resolved.typeOnly) {
2209-
program.pushOp(ReflectionOp.any);
2209+
this.resolveTypeOnlyImport(typeName, program);
22102210
return;
22112211
}
22122212

@@ -2308,6 +2308,18 @@ export class ReflectionTransformer implements CustomTransformer {
23082308
return declarationUser !== typeUser;
23092309
}
23102310

2311+
protected resolveTypeOnlyImport(entityName: EntityName, program: CompilerProgram) {
2312+
program.pushOp(ReflectionOp.any);
2313+
const typeName = ts.isIdentifier(entityName)
2314+
? getIdentifierName(entityName)
2315+
: getIdentifierName(entityName.right);
2316+
this.resolveTypeName(typeName, program);
2317+
}
2318+
2319+
protected resolveTypeName(typeName: string, program: CompilerProgram) {
2320+
program.pushOp(ReflectionOp.typeName, program.findOrAddStackEntry(typeName));
2321+
}
2322+
23112323
protected resolveTypeParameter(declaration: TypeParameterDeclaration, type: TypeReferenceNode | ExpressionWithTypeArguments, program: CompilerProgram) {
23122324
//check if `type` was used in an expression. if so, we need to resolve it from runtime, otherwise we mark it as T
23132325
const isUsedInFunction = isFunctionLike(declaration.parent);

packages/type/tests/compiler.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,25 @@ test('import types named import cjs', () => {
17471747
expect(js['user.d.ts']).toContain(`export declare type __ΩUser = any[]`);
17481748
});
17491749

1750+
test('emit typeName for type only imports', () => {
1751+
const js = transpile({
1752+
'app': `
1753+
import {type User} from './user.js';
1754+
typeOf<User>();
1755+
`,
1756+
'user': `export interface User {id: number}`
1757+
}, {
1758+
module: ModuleKind.CommonJS
1759+
});
1760+
const typeOf = typeOf2;
1761+
expect(eval(js['app.js'])).toMatchInlineSnapshot(`
1762+
{
1763+
"kind": 1,
1764+
"typeName": "User",
1765+
}
1766+
`);
1767+
});
1768+
17501769
test('import types named import typeOnly', () => {
17511770
const js = transpile({
17521771
'app': `

0 commit comments

Comments
 (0)