3 ms·
The type inference looks good. Pardon a question here: Will ES6 Code ‘just Run’ under TypeScript? I think the answer is yes for ES5, but I don’t know about ES6
by mark_l_watson 8y ago
The type inference looks good.
Pardon a question here: Will ES6 Code ‘just Run’ under TypeScript? I think the answer is yes for ES5, but I don’t know about ES6. Thanks.
- lachenmayer 8y agoGenerally that depends on how many 'strict' compiler options you enable -- and whether you consider legitimate type errors a reasonable reason to not 'just run' your code :) An example would be that classes that don't explicitly declare their fields will throw a type error when their fields are mutated (eg. `this.foo = 123`).
- jtreminio 8y agoI believe Typescript is an extension of ES6. TS is ES6 + types (and other things).
- stareatgoats 8y agoNot sure what you mean by running ES6 code under Typescript. It is my understanding that Typescript 'transpiles' to whatever flavor of Javascript you need, and then the code is executed as Javascript. If your question is whether you can just change the file extension of a working ES6 file to .ts without Typescript complaining then answer is 'yes', you should be able to do that no problem. Edit: of course there is little sense in doing nothing more than that, but it is a good starting point for decorating existing Javascript with TS features.
- Someone1234 8y agoThe biggest pain-point with renaming a ES6 file to *.TS is that TypeScript will flag every library or browser specific object you reference as missing. That isn't wrong behavior on TS's part, just getting started with TypeScript is very configuration heavy, particularly for targeting browsers.
- jahewson 8y agoYou can use the ‘allowJs’ flag to have the compiler accept unmodied JavaScript and for untyped modules you can use shorthand ambient module declarations so they use the ‘any’ type, with one line of code.
- superpermutat0r 8y agoIf your target is ES5 you will have to provide polyfills. Typescript will unfortunately typecheck even if you don't have polyfills and you'll get runtime errors with your ES5 target. They specifically said they won't provide polyfills and they Will continue to typecheck even if your target does not support the standard function.
- nikeee 8y agoIf you use classes, the compiler may complain because the class has no declared fields. The compiler will emit JS anyway.
- vimslayer 8y agoClass field declarations are a stage 3 proposal for EcmaScript, btw, and already supported in Chrome. So class Foo { foo; constructor() { this.foo = 42 } } Will be both valid ES2019 or ES2020 and TypeScript.