Title: JSON Module Type Resolution With Heterogeneous Arrays · Issue #37831 · microsoft/TypeScript · GitHub
Open Graph Title: JSON Module Type Resolution With Heterogeneous Arrays · Issue #37831 · microsoft/TypeScript
X Title: JSON Module Type Resolution With Heterogeneous Arrays · Issue #37831 · microsoft/TypeScript
Description: TypeScript Version: 3.9.0-dev.20200407 Search Terms: json module array type resolveJsonModule Code schema1.json [ { "foo": 1, "bar": 2 }, { "baz": "something" } ] schema2.json [ 1, "foo" ] import schema1 from './schema1.json'; import sch...
Open Graph Description: TypeScript Version: 3.9.0-dev.20200407 Search Terms: json module array type resolveJsonModule Code schema1.json [ { "foo": 1, "bar": 2 }, { "baz": "something" } ] schema2.json [ 1, "foo" ] import s...
X Description: TypeScript Version: 3.9.0-dev.20200407 Search Terms: json module array type resolveJsonModule Code schema1.json [ { "foo": 1, "bar": 2 }, { "baz": "something"...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/37831
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"JSON Module Type Resolution With Heterogeneous Arrays","articleBody":"\u003c!--\r\nPlease try to reproduce the issue with the latest published version. It may have already been fixed.\r\nFor npm: `typescript@next`\r\nThis is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly\r\n-->\r\n**TypeScript Version:** 3.9.0-dev.20200407\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:**\r\njson module array type resolveJsonModule \r\n\r\n**Code**\r\n\r\nschema1.json\r\n\r\n```json\r\n[\r\n {\r\n \"foo\": 1,\r\n \"bar\": 2\r\n },\r\n {\r\n \"baz\": \"something\"\r\n }\r\n]\r\n```\r\n\r\nschema2.json\r\n\r\n```json\r\n[\r\n 1,\r\n \"foo\"\r\n]\r\n```\r\n\r\n```ts\r\nimport schema1 from './schema1.json';\r\nimport schema2 from './schema2.json';\r\nconst example1: [ { foo: number; bar: number }, { baz: string; } ] = schema1;\r\nconst example2: [ number, string ] = schema2;\r\n```\r\n\r\n**Expected behavior:**\r\nI would expect this to compile correctly.\r\n\r\n**Actual behavior:**\r\nThere are two compile errors on assignment.\r\n\r\n```\r\nType '({ foo: number; bar: number; baz?: undefined; } | { baz: string; foo?: undefined; bar?: undefined; })[]' is missing the following properties from type '[{ foo: number; bar: number; }, { baz: string; }]': 0, 1\r\n```\r\n\r\nand \r\n\r\n```\r\nType '(string | number)[]' is missing the following properties from type '[number, string]': 0, 1\r\n```\r\n\r\n**Playground Link:** \r\nBecause this relies on the resolveJsonModule flag, I cannot reproduce this in a typescript playground. Instead, I have created this small repository which demonstrates the failed compilation.\r\nhttps://github.com/jcgsville/typescript-json-bug-repro\r\n\r\n**Related Issues:**\r\nHere is a similar issue, but doesn't address how it infers the type of arrays.\r\nhttps://github.com/microsoft/TypeScript/issues/27913\r\n\r\n**Discussion:**\r\nI could see an argument that this is intended behavior and a known limitation of the degree of sophistication of a type inferred from JSON. However, I'd expect that the JSON module resolution would resolve it to a more strict type, and the user of the module could loosen it on assignment/usage if they want.\r\n\r\nFor example, if the type of schema1 was resolved to be `[ { foo: number; bar: number }, { baz: string } ]`, then assigning it to a looser type would succeed. See the following example\r\n\r\n```ts\r\nconst schema3: [ { foo: number; bar: number }, { baz: string; } ] = [\r\n {\r\n foo: 1,\r\n bar: 2\r\n },\r\n {\r\n baz: 'something'\r\n }\r\n];\r\nconst example3: ({ foo: number; bar: number; } | { baz: string })[] = schema3;\r\n```\r\n\r\nThere are many use cases for heterogeneous arrays, but I encountered this when trying to represent anyOf in JSON Schema in a JSON module in a typescript project, which is currently impossible to do with @types/json-schema without an `as unknown` cast.","author":{"url":"https://github.com/jcgsville","@type":"Person","name":"jcgsville"},"datePublished":"2020-04-07T21:49:49Z","url":"https://github.com/microsoft/TypeScript/issues/37831"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ac1d8550-7b57-b958-6cc0-af9476c15f15 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 81FC:96DDF:299AF49:34C0966:6B019C0A |
| html-safe-nonce | 951fdfa9f667429472d6a66958ce171cd53ad499c968cb91b77bc963a30784f8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MUZDOjk2RERGOjI5OUFGNDk6MzRDMDk2Njo2QjAxOUMwQSIsInZpc2l0b3JfaWQiOiI1MzIyMTAwMTU4MTczOTEyMDc0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 7a76a4fd5bb47178d79b21da4203bfe5290245d78f38ce4a7ed0e0134b3952b9 |
| hovercard-subject-tag | issue:596172731 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/37831/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7103121ec49376712d38de82209e10acb7d03601cfda9c687c147629cbe966a6/microsoft/TypeScript/issues/37831 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7103121ec49376712d38de82209e10acb7d03601cfda9c687c147629cbe966a6/microsoft/TypeScript/issues/37831 |
| og:image:alt | TypeScript Version: 3.9.0-dev.20200407 Search Terms: json module array type resolveJsonModule Code schema1.json [ { "foo": 1, "bar": 2 }, { "baz": "something" } ] schema2.json [ 1, "foo" ] import s... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jcgsville |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width