microsoft / microsoft/TypeScript

Add 'browser' module resolution, type checking native JS module imports

Open
#37,971 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

Disable non-relative modules
Disable absolute module path
Change node_modules folder
Disable node_modules non-relative module lookup

Suggestion

A compiler option to disable non-relative modules, since these are invalid in the browser,

May be

  • An option to disable node_modules lookup
  • Or a new moduleResolution option: browser, with the same behavior as node but without looking into node_modules and allowing only *.js imports

Use Cases

Now that native JS modules are becoming popular is possible to create web apps without any bundling and/our module path rewriting. Currently this is possible but typescript allows non-relative modules, leaving the task of checking all imports for any invalid path to the user.

Here is stated that per design one should write the import path that works at runtime, so is sensible to ask for a way to check for path validation considering that the code will run as-is on the browser

Examples

//No problem! Native JS modules supported via pathMapping:
import * from "/web_modules/redux.js"

//ok too
import * from "/src/ui/button.js"; 

//wrong! Non-relative modules are not supported in browser, but typescript allows it (should raise compiler error)
import * from "react";

///wrong! An import without '.js' extension will not be resolved correctly by the browser
import * from "/src/ui/button"

Checklist

My suggestion meets these guidelines:

  • [ X ] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [ X ] This wouldn't change the runtime behavior of existing JavaScript code
  • [ X ] This could be implemented without emitting different JS based on the types of the expressions
  • [ X ] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [ X ] This feature would agree with the rest of TypeScript's Design Goals.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the proposed browser module-resolution behavior and the import examples in this issue. Determine how a new compiler option should treat node_modules lookup, non-relative imports, and missing .js extensions. Done means the compiler can validate browser-native module imports without changing existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.