-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
JavaScript types in TypeScript #4790
Comments
If you do decide to be compatible with Closure Compiler's annotations, please also consider letting TS parse its nullability qualifiers ( |
EDIT: Fixed in 1.8, can disregard the rest of this comment now.... Another case that might need consideration here is ES7 async/await: /**
* @return {CustomPromise}
*/
async function test(){
return 42;
}
let result = test(); // native Promise or CustomPromise? Running this in a JavaScript engine that supports async/await (eg in MS Edge) will assign an ES6 Promise instance to But what if this was run through the TypeScript compiler first, e.g. using Return type annotations on TypeScript |
this is fixed in 1.8, see #6581 |
This may sound crazy, but one extra way to define the type of a variable would be using labels. The following is perfectly valid code and conveniently works with existing JS syntax highlighting: String: var foo;
String: var bar;
Number: var baz;
Object: var biz;
CustomObject: var fee;
// TypeScript should throw errors for these
foo = 1;
bar = 2;
baz = 'asd';
biz = 'asd';
fee = []; |
Are there docs on this? I see this in the wiki but cannot figure out how to enable this. A js file included by virtue of |
There are no errors reported in the js files at this time. Only consuming them; I.e. With --allowjs you can use types from a js file. Salsa is a loose term to refer to the TS compiler understanding, consuming, and providing language services in js files. I consider this largely implemented with possible enhancements in the future. Salsa is what derives the js IDE experience in VSCode today and some project contexts in VS as well (e.g. Node tools). |
I have a large JS project and I was quite surprised by this. Of course I understand the challenges here but am I correct that at this time there's no way to get type check errors in a pure JS project? Is this on the roadmap? |
JavaScript types in TypeScript
This write-up outlines considerations for potential integration of JavaScript and TypeScript at the type system level. (See parent issue #4789 for an overview)
Basic inference as it works today
Several constructs in JavaScript will be recognized and typed using TypeScript inference today, for example:
There are several limitations however that the TypeScript type annotations were designed to support. For example:
Here there is no way to infer the types of 'a' or 'b' (and thus also the return type), and no way in the JavaScript syntax to specify the parameter types. For simple cases, this case be accurately and equivelently modeled using JsDoc annotations. For example:
Which is identical in meaning to the following TypeScript:
(Note that in this simple case the return type is not necessary, and could have been inferred once the paramater types were known).
Below shows some more JsDoc annotations that model similar TypeScript constructs closely, such as union types, object literal types, optional types, "any" types, generic types, rest args, referencing declared types, etc...
Consuming and providing types
Similar to the way TypeScript works today, files are included in a project "context", and the values and types in that context are visible across files (according to the rules on scope and visibility). For example, the interface
foo
may be declared in filefoo.d.ts
, which may be included in the project context via the command-line, tsconfig.json, or a/// <reference ..>
line at the start of the file. JsDoc annotations would then be able to reference the typefoo
in the JavaScript files.Similarly, the JavaScript file may declare a value
test
with a type of{name: string, age: number}
, and the identifiertest
would then be available to TypeScript files in the same context with the type given.Beyond the simple scenarios for values and types given above, this also extends to the module system, where code authored in JavaScript may import a module written in TypeScript, and vice-versa.
TODOs and open questions:
The text was updated successfully, but these errors were encountered: