Skip to content

Commit

Permalink
Type-only namespace imports
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Nov 20, 2019
1 parent acbee11 commit ab31628
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2227,7 +2227,8 @@ namespace ts {

function getTargetOfNamespaceImport(node: NamespaceImport, dontResolveAlias: boolean): Symbol | undefined {
const moduleSpecifier = node.parent.parent.moduleSpecifier;
return resolveESModuleSymbol(resolveExternalModuleName(node, moduleSpecifier), moduleSpecifier, dontResolveAlias, /*suppressUsageError*/ false);
const moduleSymbol = resolveESModuleSymbol(resolveExternalModuleName(node, moduleSpecifier), moduleSpecifier, dontResolveAlias, /*suppressUsageError*/ false);
return moduleSymbol && node.parent.isTypeOnly ? createTypeOnlySymbol(moduleSymbol) : moduleSymbol;
}

// This function creates a synthetic symbol that combines the value side of one symbol with the
Expand Down
34 changes: 34 additions & 0 deletions tests/baselines/reference/importClause_namespaceImport.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/b.ts(2,1): error TS2708: Cannot use namespace 'types' as a value.
/b.ts(3,1): error TS2708: Cannot use namespace 'types' as a value.
/b.ts(4,14): error TS2694: Namespace '"/a"' has no exported member 'Value'.
/b.ts(5,7): error TS2741: Property 'a' is missing in type '{}' but required in type 'A'.
/b.ts(6,7): error TS2741: Property 'b' is missing in type '{}' but required in type 'B'.


==== /a.ts (0 errors) ====
export class A { a!: string }
export class B { b!: number }
export type C<T> = T;
export const Value = {};

==== /b.ts (5 errors) ====
import type * as types from './a';
types;
~~~~~
!!! error TS2708: Cannot use namespace 'types' as a value.
types.Value;
~~~~~
!!! error TS2708: Cannot use namespace 'types' as a value.
let v: types.Value;
~~~~~
!!! error TS2694: Namespace '"/a"' has no exported member 'Value'.
const a: types.A = {};
~
!!! error TS2741: Property 'a' is missing in type '{}' but required in type 'A'.
!!! related TS2728 /a.ts:1:18: 'a' is declared here.
const b: types.B = {};
~
!!! error TS2741: Property 'b' is missing in type '{}' but required in type 'B'.
!!! related TS2728 /a.ts:2:18: 'b' is declared here.
const c: types.C<string> = "";

43 changes: 43 additions & 0 deletions tests/baselines/reference/importClause_namespaceImport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//// [tests/cases/conformance/externalModules/typeOnly/importClause_namespaceImport.ts] ////

//// [a.ts]
export class A { a!: string }
export class B { b!: number }
export type C<T> = T;
export const Value = {};

//// [b.ts]
import type * as types from './a';
types;
types.Value;
let v: types.Value;
const a: types.A = {};
const b: types.B = {};
const c: types.C<string> = "";


//// [a.js]
"use strict";
exports.__esModule = true;
var A = /** @class */ (function () {
function A() {
}
return A;
}());
exports.A = A;
var B = /** @class */ (function () {
function B() {
}
return B;
}());
exports.B = B;
exports.Value = {};
//// [b.js]
"use strict";
exports.__esModule = true;
types;
types.Value;
var v;
var a = {};
var b = {};
var c = "";
42 changes: 42 additions & 0 deletions tests/baselines/reference/importClause_namespaceImport.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
=== /a.ts ===
export class A { a!: string }
>A : Symbol(A, Decl(a.ts, 0, 0))
>a : Symbol(A.a, Decl(a.ts, 0, 16))

export class B { b!: number }
>B : Symbol(B, Decl(a.ts, 0, 29))
>b : Symbol(B.b, Decl(a.ts, 1, 16))

export type C<T> = T;
>C : Symbol(C, Decl(a.ts, 1, 29))
>T : Symbol(T, Decl(a.ts, 2, 14))
>T : Symbol(T, Decl(a.ts, 2, 14))

export const Value = {};
>Value : Symbol(Value, Decl(a.ts, 3, 12))

=== /b.ts ===
import type * as types from './a';
>types : Symbol(types, Decl(b.ts, 0, 11))

types;
types.Value;
let v: types.Value;
>v : Symbol(v, Decl(b.ts, 3, 3))
>types : Symbol(types, Decl(b.ts, 0, 11))

const a: types.A = {};
>a : Symbol(a, Decl(b.ts, 4, 5))
>types : Symbol(types, Decl(b.ts, 0, 11))
>A : Symbol(types.A)

const b: types.B = {};
>b : Symbol(b, Decl(b.ts, 5, 5))
>types : Symbol(types, Decl(b.ts, 0, 11))
>B : Symbol(types.B)

const c: types.C<string> = "";
>c : Symbol(c, Decl(b.ts, 6, 5))
>types : Symbol(types, Decl(b.ts, 0, 11))
>C : Symbol(types.C, Decl(a.ts, 1, 29))

47 changes: 47 additions & 0 deletions tests/baselines/reference/importClause_namespaceImport.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
=== /a.ts ===
export class A { a!: string }
>A : A
>a : string

export class B { b!: number }
>B : B
>b : number

export type C<T> = T;
>C : T

export const Value = {};
>Value : {}
>{} : {}

=== /b.ts ===
import type * as types from './a';
>types : any

types;
>types : any

types.Value;
>types.Value : any
>types : any
>Value : any

let v: types.Value;
>v : any
>types : any

const a: types.A = {};
>a : import("/a").A
>types : any
>{} : {}

const b: types.B = {};
>b : import("/a").B
>types : any
>{} : {}

const c: types.C<string> = "";
>c : string
>types : any
>"" : ""

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @Filename: /a.ts
export class A { a!: string }
export class B { b!: number }
export type C<T> = T;
export const Value = {};

// @Filename: /b.ts
import type * as types from './a';
types;
types.Value;
let v: types.Value;
const a: types.A = {};
const b: types.B = {};
const c: types.C<string> = "";

0 comments on commit ab31628

Please sign in to comment.