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 schema key can only be string #6

Open
tonyxiao opened this issue Apr 5, 2017 · 1 comment
Open

Json schema key can only be string #6

tonyxiao opened this issue Apr 5, 2017 · 1 comment

Comments

@tonyxiao
Copy link

tonyxiao commented Apr 5, 2017

{
  $schema: 'http://json-schema.org/schema#',
  type: 'object',
  additionalProperties: true,
}

Currently gets converted into

export type = { [key: any]: any;
};

However, in reality json keys can ONLY be string. So the correct type should be

export type = { [key: string]: any;
};
@dannynelson
Copy link
Contributor

Thanks for the feedback, @tonyxiao.

You are correct that JSON keys (and all javascript keys) are strings. On the other hand, since javascript coerces keys to strings, a key of any type becomes valid JSON. For example:

type Obj = {[key: string]: any};
const obj: Obj = {'3': 'test'};
console.log(obj[3]); // coerced into "3" and returns "test", but flow errors "number is incompatible with string"

Thoughts?

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