镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

import defer "./a.js" is accepted without an error, and the output drops defer #64627

Description

@leonidaz

🔎 Search Terms

import defer, deferred import, side-effect import, import phase

🕗 Version & Regression Information

  • This is the behavior in every version I tried: 5.9.3 and 7.1.0-dev.20261004.1.

⏯ Playground Link

https://www.typescriptlang.org/play/?module=99&target=99#code/JYWwDg9gTgLgBAEwKYDMlTgIgHQHoCG2AVgM6YDcAUEA

💻 Code

import defer "./a.js";

🙁 Actual behavior

No error. The output leaves out defer, so the module is evaluated eagerly:

import "./a.js";

🙂 Expected behavior

An error. The import defer proposal only allows a namespace import after defer (import defer * as ns from "./a.js"), so this is a syntax error in JavaScript. Babel rejects it (Unexpected token, expected "{").

TypeScript 7.1 already reports an error for the same form with the source phase:

import source "./b.wasm";
// error TS18111: A source phase import must specify a local binding.

Additional information about the issue

In tryParseImportClause, a phase without bindings keeps an empty import clause only for source. For defer, the function returns nil, so the declaration has no import clause, and its phase is lost. checkGrammarImportClause also has no check for a deferred import without bindings: it reports a default import (TS18058) and named imports (TS18059) only.

Keeping the empty import clause for defer too, and reporting an error for it in checkGrammarImportClause, would match import source "./b.wasm".

Found while matching TypeScript's diagnostics in TSRX's parser.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions