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

JSON Module members not considered part of string literal type #40189

Closed
wegry opened this issue Aug 21, 2020 · 2 comments
Closed

JSON Module members not considered part of string literal type #40189

wegry opened this issue Aug 21, 2020 · 2 comments

Comments

@wegry
Copy link

wegry commented Aug 21, 2020

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.
@MartinJohns
Copy link
Contributor

You want #32063.

@wegry
Copy link
Author

wegry commented Aug 22, 2020

Closing as duplicate of #32063.

@wegry wegry closed this as completed Aug 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants