microsoft / microsoft/TypeScript
`import './file.json'`: This module is declared with using 'export ='
Dieses Issue hat noch niemand übernommen.
- 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:
allowSyntheticDefaultImports: trueworks as expected, but that flag is unsafe and I don't want (need) to use it.-
declare module './list.json' { const list: string[]; export default list; }
Playground Link: repro.zip
Related Issues: https://github.com/microsoft/TypeScript/issues/15146
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Ö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