Closed
Description
TypeScript Version: 4.0.2 and 4.1.0-dev.20200821
Search Terms:
json module tagged union typescript
json module string literal type typescript
brings up https://stackoverflow.com/questions/54352128/typescript-convert-string-in-json-to-string-literal with no answer.
Code
https://github.com/wegry/json-modules-and-string-literal-types
// index.ts
import * as json from './index.json'
type Schema = {
$schema: {
declarations: Record<string, { type: 'string' | 'number' }>
}
}
const usage: Schema = json
console.log(usage)
with a index.json
like so.
{
"$schema": {
"declarations": {
"name": {
"type": "string"
}
}
}
}
Expected behavior:
tsc
should reconcile "string" to be a member of the string literal type in defined at $schema.declarations.name.type
.
Actual behavior:
$ tsc
index.ts:9:7 - error TS2322: Type '{ $schema: { declarations: { name: { type: string; }; }; }; }' is not assignable to type 'Schema'.
The types of '$schema.declarations' are incompatible between these types.
Type '{ name: { type: string; }; }' is not assignable to type 'Record<string, { type: "string" | "number"; }>'.
Property '"name"' is incompatible with index signature.
Type '{ type: string; }' is not assignable to type '{ type: "string" | "number"; }'.
Types of property 'type' are incompatible.
Type 'string' is not assignable to type '"string" | "number"'.
9 const usage: Schema = json
~~~~~
Found 1 error.
Metadata
Metadata
Assignees
Labels
No labels