Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't declare local variables with dynamic(!) imported types #22293

Closed
STARSCrazy opened this issue Mar 2, 2018 · 3 comments
Closed

Can't declare local variables with dynamic(!) imported types #22293

STARSCrazy opened this issue Mar 2, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@STARSCrazy
Copy link

Bug
It isn't possible to declare a local variable with an dynamic imported class/type.

TypeScript Version:
2.7.1 (visual studio tools)

Code

//Module1.ts
export class Class1 {
}

//Module2.ts
export class Class2 {
	public async TestMethod() {
		const module1 = await import("./Module1");
		var instance = new module1.Class1(); //works fine: Variable has type 'Class1'' (implicitly typed)
		var variable1: module1.Class1; //doesn't work!!! Message: Cannot find namespace "module1"
	}
}

Expected behavior:
The code compiles without errors. It's allowed to declare a variable with an dynamic imported type, because it works with implicitly typed local variables and non-dynamic imported module types, too.

Actual behavior:
For the variable1 declaration: Cannot find namespace "module1"

@STARSCrazy STARSCrazy changed the title Dynamic Import Expression: Can't declare variables with imported types Can't declare local variables with dynamic(!) imported types Mar 2, 2018
@ghost
Copy link

ghost commented Mar 2, 2018

You can just import { Class1 as Class1Type } from "module1"; at the top, and as long as you don't use that Class1Type as a value, it won't be in the emitted code. Although it would be nicer if we let you write import { type Class1 } from "module1"; and guaranteed that it wouldn't be used as a value.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 2, 2018

module1 is a value. a value does not have a type or namespace capabilities. this is one of the basic assumptions that the compiler makes about declarations.. see http://www.typescriptlang.org/docs/handbook/declaration-merging.html for more details.

I think the underlying issue of referring to a type from a module is tracked by
#14844

@mhegazy mhegazy added the Duplicate An existing issue was already created label Mar 2, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants