Repository navigation
JavaScript in TypeScript compilations #4792
Copy link
Copy link
Closed
Labels
FixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
Description
Activity
- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptIn DiscussionNot yet reached consensusNot yet reached consensus
on Sep 14, 2015 Here are some opinions:
- the environment should provide syntactic support for ES5 (or lower?) but should not compile. Alternatively provide a compiler flag to ignore (i.e not compile). There are reason to want to NOT re-compile third-party sources.
- definitely support transpilation of es6 files unless an alternative transpiler has been configured
- definitely support jsx, by default or compiler flag
- node modules should be resolved in exactly the same way that node does it, to avoid surprises (e.g. incompatible version being picked up).
- the compiler should not constrain the file extensions but should provide compilation or configuration options to specify extensions to be processed
- tsconfig file should not automatically process all files in its directory and below, but should respect the configured extensions mentioned above. Also consider supporting an 'exclude' configuration, .i.e. exclude files/directories specified by name or pattern/regex
- jsx treated as .tsx, hmm, first thought is probably yes
- copy input js to outDir even if not transpiled - my instinct says no, don't do unnecessary work, also because the environment or third-party tools may have a 'watch' on the directory. I'm thinking of something like webpack watching the directory for bundling; don't want it to do something when nothing has changed.
- explicit list of files to compile is ok for small projects but leads to busy work and increases the potential for errors in large projects; i'm not a fan
- emitting .d.ts should be done on-demand, not automatically unless the compiler is smart enough to differentiate interface changes from body changes; comprise by adding a compiler flag
Side note: I've experience Visual Studio Code erroring on es6 files unless the 'target' compiler option was set to es6. It should be using a 'source' specifier; target is what we want to transpile to.
This should be available now with #5471 using
--allowJs- addedFixedA PR has been merged for this issueA PR has been merged for this issueand removedIn DiscussionNot yet reached consensusNot yet reached consensus
on Nov 20, 2015 Any usage docs?
Reacted by strohhut, Aaron Hamid, Oskar Klintrot, robmcm and Rafid K.I think the support of globs in tsconfig.json file is really necessary for this feature to be useful. How do I keep typescript from pulling in a lot of devtime dependencies (like gulp modules)?
specify "exclude" property in your tsconfig.
- locked and limited conversation to collaborators
on Jun 19, 2018
Metadata
Metadata
Assignees
Labels
FixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript
JavaScript in TypeScript compilations
(See parent issue #4789 for an overview)
There are a number of considerations for the TypeScript compiler to handle JavaScript code:
targetECMAScript mode, or theJSXflags for handling React/JSX code.node_modulesfolder?express) already has a.d.tsdeclaration, and crawling the JavaScript files is unnecessary?.js, or can other file types be provided (e.g..es)?JavaScript files for input
There are four ways in which source files may be included in a compilation:
tsconfig.jsonfile exists in a folder that is compiled, and it does not explicitly list files to include. In this case, all source files in this folder and subfolders are crawled for source files to compile.filesproperty in atsconfig.jsonfile, or via files listed on thetsccommand line./// <reference ...>tags in other source files.Implementation note: Currently, files are parsed and their dependencies included and parsed, before the next input file is parsed. For example, if a compilation includes
a.tsandb.ts(in that order), buta.tsimports.\c, thencwill be parsed and its dependencies resolved and included beforeb.tsis processed.Steps 1 - 3 above are unambiguous as to which file to include in the set to compile, as the file extension is part of the given or discovered file name. For step 4 (module resolution), the file extension is not given, and precedence rules are followed (module resolution will attempt to location
.ts, then.d.tsfiles for the module name. The.jsextension would go on the end of this list).Once the set of source files is gathered, the list of emit files can be calculated. If there is a collision (i.e.
src\foo.jsis both a source and emit file), then it should be removed from the list of source files if it was found via discovery (i.e. crawling the folder or resolving a module path discovered the file to be emitted). If it was listed explicitly as an input file, yet would be overwritten on emit, then an error should be raised.Transpiling JavaScript
It would be valuable to allow developers to write the same ES6 constructs they can use in TypeScript (e.g. arrow functions, classes, etc.) and provide the same conversion to ES5/ES3 that is available for TypeScript. As this should be relatively simple to implement, this would be worth adding. Of note is that due to the input/output file conflict outlined above, the emitting files would need to go to a separate folder (
--outDir), or be combined into a separate output file (--outFile) in order to avoid attempting to overwrite the source file on emit (else input and output will have the same file path).Compiler flags
There are two categories of compiler flags to consider:
Existing flags of interest include:
target: should the compiler/langauge service care about the targeted ECMAScript level (especially if not transpiling)?jsx: should the compiler/language service treat JavaScript files written in JSX syntax the same as source.tsxfiles?outDir: should the compiler copy the input JavaScript files to the output directory (especially if not transpiling)?declaration: should the compiler emit a.d.tsfile for JavaScript files it has parsed and gathered types from?module: should the compiler care about module type for.jsfiles?TODO
node_modulesjsfile extensions