New --emitExtension and --noImplicitExtensionName compiler options by Jack-Works · Pull Request #35148 · microsoft/TypeScript (original) (raw)
Related: denoland/deno#8529, #27957, #35749
fixes #30076, fixes #18442, fixes #16577, fixes #33306
See also: Node.js new ESModule support: https://medium.com/@nodejs/announcing-core-node-js-support-for-ecmascript-modules-c5d6dc29b663
Summary
Improve support for deno
and browser
style import. Note: This pr doesn't include URI style import resolution (https://deno.land/...
or std:kv-storage
...), about URI style import, see #35749
New --noImplicitExtensionName
compiler options.
With this option on, the following code becomes invalid.
This new options will enforce developer to write valid import path on deno
(noImplicitExtensionName + emitExtension=.ts
) and ES Module for browser (noImplicitExtensionName + emitExtension=.js (default value)
).
New --emitExtension
compiler options.
This flag allows tsc to emit .mjs
, .es.js
, .es
, .ts
or any extension user specifies.
tsconfig.json
{ compilerOptions: { emitExtension: ".mjs", noImplicitExtensionName: true } }
Discussions
According to #35163 (comment) maybe --noImplicitExtensionName
should be default to true
when the target module system is ESModule
Details
Due to the ts meeting note for this pr (#35589 )
I decided to rewrite ts pr to follow the ts design principle.
- Resolve TS file by
--emitExtension
- Type error for import with no extension name
- Emit file as ext of
emitExtension
- Allow import
.ts
and.tsx
file whenemitExtension
===.ts
or.tsx
- If the extension name !== emitExtension, emit a compile error
Resolve TS file by --emitExtension
Resolve type of import './x.mjs'
to the file x.ts
.
Will NOT rewrite the emitted file path.