microsoft / microsoft/TypeScript

`import './file.json'`: This module is declared with using 'export ='

Offen
#37,623 5 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

In Discussion Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

In short, JSON files's implied type definition is always defined via export =, even if I'm using module: es2015, causing it to error unless I use an unnecessary flag (allowSyntheticDefaultImports)

TypeScript Version: 3.9.0-dev.20200326

Search Terms:

import require json module commonjs es2015

Code

import list from "./list.json";

for (const item of list) {
	console.log(item)
}
{
	"compilerOptions": {
		"outDir": "distribution",
		"target": "es2017",
		"module": "es2015",
		"moduleResolution": "node",
		"resolveJsonModule": true,
		"strict": true
	},
	"files": [
		"index.ts"
	]
}

Expected behavior:

list is of type string[]

Actual behavior:

index.ts:1:8 - error TS1259: Module '"./list"' can only be default-imported using the 'allowSyntheticDefaultImports' flag

1 import list from "./list.json";
         ~~~~

  list.json:1:1
    1 [
      ~
    This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.


Found 1 error.

Workaround:

  1. allowSyntheticDefaultImports: true works as expected, but that flag is unsafe and I don't want (need) to use it.
  2. declare module './list.json' {
    	const list: string[];
    	export default list;
    }
    

Playground Link: repro.zip

Related Issues: https://github.com/microsoft/TypeScript/issues/15146

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit der im Issue verlinkten repro.zip, insbesondere mit index.ts, list.json und den tsconfig-Einstellungen für resolveJsonModule und module: es2015. Vergleichen Sie das Verhalten mit dem zugehörigen Issue #15146; als erledigt gilt die Aufgabe, wenn der gezeigte default import ohne allowSyntheticDefaultImports die Typprüfung besteht und list als string[] inferiert wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.