Repository navigation
Path resolution for URI style import #35163
Description
Activity
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:
import {utils} from 'utils.js'this is supported:
import {utils} from '/utils.js'Jack-Works commented
on Nov 24, 2019 ContributorAuthorMore actionsAlso there is no way to configure auto-import to always prepend '/' to absolute path.
This import(which VSCode produces) is not supported in browsers:
import {utils} from 'utils.js'this is supported:
import {utils} from '/utils.js'Relative import may be enough
.mjsis a second-class citizen now. Node.js 13.2 just released with unflagged module support and it's possible to enable ESM package-wide by using"type": "module"in its package.json. No need for.mjs.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
imports.--emitExtensionlooks like just what the doctor ordered for that, but it should probably also add the extension for imports without one, too. Otherwise life is about to get a lot harder for TS devs once ESM in Node starts really taking off.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.
Reacted by Timo Rossa and bouzuyaandrewbranch commented
on Dec 11, 2019 MemberMore actionsRelevant reading: #35589
- changed the title
[-]Path resolution for browser style import[/-][+]Path resolution for URI style import[/+]on Dec 18, 2019 - locked as resolved and limited conversation to collaborators
on Oct 21, 2025
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,.tsxfile is a type error (PR: New --emitExtension and --noImplicitExtensionName compiler options #35148).jsextension name (PR: New --emitExtension and --noImplicitExtensionName compiler options #35148).mjsextension 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,.tsxfile 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
.tsor a.tsxfile is no longer a type error. Importing.d.tsis still a type error.2. Emitted code does not contain the
.jsextension 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.tsor.tsxextension name for relative imports.3. Can not emit file with
.mjsextension 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
.mjsextname. 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.jsso TypeScript maybe should not restrict the possible value of--emitExtensionwith 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
pathcan 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
uriPathsthat 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:
{ "compilerOptions": { "uriPaths": { "https://unpkg.com/(.+?)@.+?/.+": "./node_modules/@types/$1", "https://deno.land/(.+?)@v.+?/(.+?)/(.+)": "$DENO_DIR/deps/https/deno.land/*", "std:(.+)": "./node_modules/builtin-module-types/$1" } } }This rule map https://unpkg.com/[email protected]/lodash.js to
@types/lodash-esMap https://deno.land/[email protected]/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: nodeThe folder import is only supported in Node.js and some bundle tool, browsers and deno doesn't support it.ProposalAdd a flag--noFolderImportto 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
--noImplicitExtensionNameto 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.