microsoft / microsoft/TypeScript

[beta] Default imports behave as namespace imports for JSON modules with allowArbitraryExtensions

オープン
#52,863 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

@weswigham がすでに取り組んでいます。

2023年2月25日 から。

Needs Investigation Rescheduled
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Bug Report

🔎 Search Terms

allowArbitraryExtensions, json modules, default import

🕗 Version & Regression Information

This is a problem with the new feature allowArbitraryExtensions in Typescript 5.0 beta.

⏯ Playground Link

This is a multi file issue so can't be replicated on the Playground.

💻 Code

TSconfig:

{
    "include": [
        "./**/*.ts",
        "./**/*.mts",
        "./**/*.cts",
        "./**/*.json"
    ],
    "exclude": [
        "./**/*.d.ts",
        "./**/*.d.mts",
        "./**/*.d.cts"
    ],
    "compilerOptions": {
        "composite": true,
        "declaration": true,
        "declarationMap": true,
        "downlevelIteration": true,
        "isolatedModules": true,
        "module": "NodeNext",
        "rootDir": "./",
        "sourceMap": true,
        "target": "esnext",
        "newLine": "lf",
        "useDefineForClassFields": true,
        "strict": true,
        "alwaysStrict": true,
        "exactOptionalPropertyTypes": true,
        "noImplicitOverride": true,
        "noUncheckedIndexedAccess": true,
        "importsNotUsedAsValues": "error",
        "forceConsistentCasingInFileNames": true,
        "allowArbitraryExtensions": true
    }
}

JSON file:

// mimeMap.json
{
    "text/javascript": ".js",
    "text/css": ".css",
    "text/html": ".html"
}

Declaration file for JSON:

// mimeMap.d.json.ts
declare const mimeMap: Record<string, string>;
export default mimeMap;

TS file:

import mimeMap from "./mimeMap.json" assert { type: "json" };

const mimeType = "some/mimetype";
const ext = mimeMap[mimeType];
🙁 Actual behavior

We get an error at mimeMap[mimeType]:

Element implicitly has an 'any' type because expression of type '"some/mimetype"' can't be used to index type 'typeof import("/home/jamesernator/projects/ts-issue/mimeMap.d.json", { assert: { "resolution-mode": "import" } })'.
  Property 'some/mimetype' does not exist on type 'typeof import("/home/jamesernator/projects/ts-issue/mimeMap.d.json", { assert: { "resolution-mode": "import" } })'.ts(7053)

This occurs because mimeMap has type { default: Record<string, string> } rather than just Record<string, string> as it should be.

🙂 Expected behavior

The JSON import should behave as a default import, not import the whole namespace as the default import. This worked correctly for other file types (other than .json) so I presume there is a conflict with the fact TS already has JSON module support (with resolveJsonModule).

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。