Skip to content

String union type lost when importing from json #27304

Closed
@bali182

Description

@bali182

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

Activity

s-ve

s-ve commented on Sep 24, 2018

@s-ve

It's been discussed in #26552

typescript-bot

typescript-bot commented on Dec 14, 2018

@typescript-bot
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @s-ve@bali182@RyanCavanaugh@typescript-bot

        Issue actions

          String union type lost when importing from json · Issue #27304 · microsoft/TypeScript