You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(I posted this on StackOverflow first, but got no response. So either this is a hard question or a stupid one, but I'm too new at TypeScript to be able to judge. So please excuse me if the latter case applies).
I am writing a TypeScript Type definition for a pretty large (800+ files) AMD JavaScript library. Below you can see a snippet:
I then try to write a TypeScript application that depends on this type definition file:
/// <reference path="./myjslib.d.ts" />
import Point = require("myjslib/shape/Point");
import Circle = require("myjslib/shape/Circle");
import Circ = myjslib.shape.Circle;
let c1 = new Circle(new Point(1,3), 20);
let c2: Circle;
let c3: myjslib.shape.Circle;
let c4: Circ;
c1.contains(new Point(5,6));
c2 = c1;
c3 = c1;
c4 = c1;
When compiling this typescript file, the correct JavaScript code is generated: the output is valid AMD, can be loaded with an off the shelf AMD module loader, and can be optimized into a single JavaScript file, etc.
However, the TypeScript compiler (I am currently using version 1.8.10) generates an error at the declaration of the c2 variable:
Apparently, it is possible to refer to the imported Circle to create a circle instance. The import statement works as you have access to the Circle constructor. However, Circle cannot be used as a type annotation when declaring a variable of that type. What does work however is using the fully qualified name of the type: the compiler does not complain about the c3 declaration, so the typescript definition file is found and used by the compiler. Also, if I create an alias Circ, The type system works like a charm (declaration of c4).
It was my understanding that the import Circle = require("myjslib/shape/Circle") statement created a local alias for the Circle entity but this does not appear to be the case.
So in conclusion: I am currently writing code where I have to use the import BindingIdentifier to create instances of a particular class, but I have to use the fully qualified type name if I want to declare variables of that class. This makes writing code pretty confusing (and verbose). Is there any way around this? Is this the expected behavior, or am I running into a TypeScript compiler bug?
The text was updated successfully, but these errors were encountered:
the issue is in the use of export =. this is rather subtle, and it should be fixed on the compiler side at some point. the issue is export=<identifier> exports all meaning of an identifier, where as export=<dotted name> does not. see #4325.
(I posted this on StackOverflow first, but got no response. So either this is a hard question or a stupid one, but I'm too new at TypeScript to be able to judge. So please excuse me if the latter case applies).
I am writing a TypeScript Type definition for a pretty large (800+ files) AMD JavaScript library. Below you can see a snippet:
I have defined types in namespaces. (note that myjslib is not the real name of the library :)):
https://www.typescriptlang.org/docs/handbook/writing-declaration-files.html). Class names like
Point
andCircle
are likely to clash if my library is combined with other JavaScript libraries.I then try to write a TypeScript application that depends on this type definition file:
When compiling this typescript file, the correct JavaScript code is generated: the output is valid AMD, can be loaded with an off the shelf AMD module loader, and can be optimized into a single JavaScript file, etc.
However, the TypeScript compiler (I am currently using version 1.8.10) generates an error at the declaration of the
c2
variable:Apparently, it is possible to refer to the imported
Circle
to create a circle instance. The import statement works as you have access to theCircle
constructor. However,Circle
cannot be used as a type annotation when declaring a variable of that type. What does work however is using the fully qualified name of the type: the compiler does not complain about thec3
declaration, so the typescript definition file is found and used by the compiler. Also, if I create an aliasCirc
, The type system works like a charm (declaration ofc4
).It was my understanding that the
import Circle = require("myjslib/shape/Circle")
statement created a local alias for the Circle entity but this does not appear to be the case.So in conclusion: I am currently writing code where I have to use the import BindingIdentifier to create instances of a particular class, but I have to use the fully qualified type name if I want to declare variables of that class. This makes writing code pretty confusing (and verbose). Is there any way around this? Is this the expected behavior, or am I running into a TypeScript compiler bug?
The text was updated successfully, but these errors were encountered: