microsoft / microsoft/TypeScript

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

Open
#52,863 0 comments 0 reactions 1 assignee View on GitHub

@weswigham is already working on this.

Since Feb 25, 2023.

Needs Investigation Rescheduled
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

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).

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.