Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String union type lost when importing from json #27304

Closed
bali182 opened this issue Sep 24, 2018 · 2 comments
Closed

String union type lost when importing from json #27304

bali182 opened this issue Sep 24, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@bali182
Copy link

bali182 commented Sep 24, 2018

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

@s-ve
Copy link

s-ve commented Sep 24, 2018

It's been discussed in #26552

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 24, 2018
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants