-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Path resolution for URI style import #35163
Comments
Also there is no way to configure auto-import to always prepend '/' to absolute path. This import(which VSCode produces) is not supported in browsers: this is supported: |
Relative import may be enough |
But that said... extensionless imports are a problem. That issue used to just be limited to the browser (and deno), but the default mode in Node 13.2 turns out to be to require the extension for all file-based https://nodejs.org/api/esm.html#esm_customizing_esm_specifier_resolution_algorithm |
I think the default behavior of TSC should be to emit valid JavaScript. At the moment, especially with the NodeJS 13.2 change, TypeScript will emit invalid JavaScript by default. // valid typescript
import { Foo } from './foo'
new Foo()
// emitted javascript
import { Foo } from './foo'; // invalid in browser and NodeJS!
new Foo();
// invalid typescript
import { Foo } from './foo.ts' // invalid in TS
new Foo()
// emitted javascript
import { Foo } from './foo.ts'; // invalid in browser and NodeJS!
new Foo(); If the user is targeting ES modules, an extension should be included by default. Otherwise, the default behavior of TSC will be to generate invalid JS which goes against the ethos of TypeScript. |
Relevant reading: #35589 |
There are some use cases that use tsc to emit ESModule that runs in the browser directly. Currently, TypeScript is not friendly to browser style import environment like browser and deno.
There are some main problems when using TypeScript in those environments:
.ts
,.tsx
file is a type error (PR: New --emitExtension and --noImplicitExtensionName compiler options #35148).js
extension name (PR: New --emitExtension and --noImplicitExtensionName compiler options #35148).mjs
extension name (PR: New --emitExtension and --noImplicitExtensionName compiler options #35148)Should treat folder import (import './src'
=>import './src/index.ts'
) as type error in some environmentsimport './src'
=>import './src.ts'
) as type error in some environments1. Import
.ts
,.tsx
file is a type errorRelated issues: #30076
This happens in the deno. Deno will not try to resolve implicit extension name when handling imports.
This import is invalid for deno and browser.
In browser we have to write
import "./file.js";
which is valid in the type checker.In deno we have to write
import "./file.ts";
which is invalid in the type checker. TypeScript reportsAn import path cannot end with a '.ts' extension. Consider importing './file' instead
#35148 introduce a change that importing a
.ts
or a.tsx
file is no longer a type error. Importing.d.ts
is still a type error.2. Emitted code does not contain the
.js
extension nameRelated issues: #16577, #30076
When tsc is used to emit ESModule for browsers, the extname is not added. Now one can write
import './file.js'
to refer tofile.ts
, that behavior is strange in the semantic.#35148 introduce a new compilerOption called
--emitExtension
. With this option, TypeScript will replace the.ts
or.tsx
extension name for relative imports.3. Can not emit file with
.mjs
extension nameRelated issues: #30076, #18442
In Node.js, there is a discussion about how to migrate to ES Module, a solution requires all ES Module files have to have a
.mjs
extname. There is no way to emit such code by tsc now.#35148 introduce a new compilerOption called
--emitExtension
. With this option, TypeScript will emit the code with the specified extension name.There are lots of different solutions that requires different extension name for js file like
.es
,.mjs
,.es.js
so TypeScript maybe should not restrict the possible value of--emitExtension
with a enum.4. Can not handle the type resolution for URI import finely
Related issues: #28985, #19942
In browser and deno, import from a "package" is different from the node style.
Currently there is no way to let TypeScript automatically map the URI to another place like
@types/*
or$DENO_DIR/deps/https/deno.land/*
The current
path
can map a simple pattern of import module specifier to another place, but in the URI style import, a more flexible way to map the URI is required.Proposal
(maybe add a new
moduleResolution: browser
)Add a new
uriPaths
that allows to map from a RegExp to a local path. It will NOT effect the emitted code. Just a way to find the type definition for those URIs.Example:
This rule map https://unpkg.com/lodash-es@4.17.15/lodash.js to
@types/lodash-es
Map https://deno.land/std@v0.24.0/http/server.ts to
$DENO_DIR/deps/https/deno.land/std/http/server.ts
.$DENO_DIR is an environment variable.
By default, on Windows, it's
~\AppData\Local\deno\deps\https\deno.land\std\http\server.ts
.By default on Linux, it is
~/.deno/deps/https/deno.land/std/http/server.ts
.5. Should treat folder import (import './src'
=>import './src/index.ts'
) as type error in some environments*: Folder import is supported only in
moduleResolution: node
The folder import is only supported in Node.js and some bundle tool, browsers and deno doesn't support it.ProposalAdd a flag--noFolderImport
to ban the folder import.6. Should treat import without extension name (
import './src'
=>import './src.ts'
) as type error in some environmentsThe implicit extension name is only supported in Node.js and some bundle tool, browsers and deno doesn't support it.
Proposal
Add a flag
--noImplicitExtensionName
to ban the implicit import.There are 6 problems when tsc is used with browser native ESModule or deno. The first one to the third one is fixed by #35148.
I have interest to do the 4 to 6. But I should make sure TypeScript team will accept those.
The text was updated successfully, but these errors were encountered: