Closed
Description
When importing from a json file, some type information seems to be lost. Would it be worth to keep the strictest type information about the imported type? Eg.: in the example instead of inferring the { type: string }
maybe infer { type: "frog" }
instead?
TypeScript Version: 3.0.3
Search Terms:
json import string union type
Code
sample.json
{
"type": "frog"
}
sample.ts
import sample from './sample.json'
type AnimalType = 'frog' | 'cat' | 'dog'
type Animal = { type: AnimalType }
const a: Animal = sample
Expected behavior:
No compliation error
Actual behavior:
Compile error: Type '{ "type": string; }' is not assignable to type 'Animal'.
Types of property 'type' are incompatible.
Type 'string' is not assignable to type 'AnimalType'.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
s-ve commentedon Sep 24, 2018
It's been discussed in #26552
typescript-bot commentedon Dec 14, 2018
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.