Title: Storing a keyof to a string quoted number key produces TS2344: invalid constraint in declaration file · Issue #37292 · microsoft/TypeScript · GitHub
Open Graph Title: Storing a keyof to a string quoted number key produces TS2344: invalid constraint in declaration file · Issue #37292 · microsoft/TypeScript
X Title: Storing a keyof to a string quoted number key produces TS2344: invalid constraint in declaration file · Issue #37292 · microsoft/TypeScript
Description: TypeScript Version: 3.8.3 and 3.9.0-dev.20200309 Search Terms: [keyof quote], [keyof number declaration], [key number], [key quote], [object key number declaration] Code // test.ts class C { readonly lines = { '1': 'abc', '2a': 'def' } }...
Open Graph Description: TypeScript Version: 3.8.3 and 3.9.0-dev.20200309 Search Terms: [keyof quote], [keyof number declaration], [key number], [key quote], [object key number declaration] Code // test.ts class C { readon...
X Description: TypeScript Version: 3.8.3 and 3.9.0-dev.20200309 Search Terms: [keyof quote], [keyof number declaration], [key number], [key quote], [object key number declaration] Code // test.ts class C { readon...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/37292
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Storing a keyof to a string quoted number key produces TS2344: invalid constraint in declaration file","articleBody":"**TypeScript Version:** 3.8.3 and 3.9.0-dev.20200309\r\n\r\n**Search Terms:** [keyof quote], [keyof number declaration], [key number], [key quote], [object key number declaration]\r\n\r\n**Code**\r\n\r\n```ts\r\n// test.ts\r\nclass C {\r\n readonly lines = {\r\n '1': 'abc',\r\n '2a': 'def'\r\n }\r\n}\r\n\r\nclass Ref\u003cT extends keyof C['lines']> {\r\n private _key: T;\r\n private _c = new C();\r\n\r\n constructor(key: T) {\r\n this._key = key;\r\n }\r\n\r\n value(): C['lines'][T] {\r\n return this._c.lines[this._key];\r\n }\r\n}\r\n\r\nconst r1 = new Ref('1');\r\nconst r2 = new Ref('2a');\r\n```\r\n\r\n**Expected behavior:**\r\n1. Run `tsc --declaration test.ts`\r\n2. Run `npx tsc --noEmit test.d.ts`\r\n3. No error is emitted\r\n\r\n**Actual behavior:**\r\nStep (2) actually produces an error:\r\n\r\n2. Run `npx tsc --noEmit test.d.ts`\r\n```\r\ntest.d.ts:13:23 - error TS2344: Type '\"1\"' does not satisfy the constraint '1 | \"2a\"'.\r\n\r\n13 declare const r1: Ref\u003c\"1\">;\r\n```\r\n\r\nWhen examining the output, note the two lines marked \"BUG\": the key in the `lines` object for `1` has been converted to number, but the `Ref\u003c\"1\">` is trying to access the key as a string.\r\n\r\n```ts\r\n// test.d.ts\r\ndeclare class C {\r\n readonly lines: {\r\n 1: string; // BUG\r\n '2a': string;\r\n };\r\n}\r\ndeclare class Ref\u003cT extends keyof C['lines']> {\r\n private _key;\r\n private _c;\r\n constructor(key: T);\r\n value(): C['lines'][T];\r\n}\r\ndeclare const r1: Ref\u003c\"1\">; // BUG\r\ndeclare const r2: Ref\u003c\"2a\">;\r\n```\r\n\r\nWhen tsc un-quotes the numeric key in `C`'s definition, the `Ref\u003c\"1\">` produces a type mismatch.\r\n\r\n**Playground Link:** [Playground Link](https://www.typescriptlang.org/play/index.html?ssl=1&ssc=1&pln=23&pc=1#code/MYGwhgzhAEDC0G8BQ1XQE4FMwBMD2AdiAJ7QgCWBmMAvIimowOQCMTAXNE2AEbBMAaBo1RMATGA5ccmAGZNh0AL5IVSUJBgAlOQB4AKtEwAPAC6YCOGAGtMxPLLgBtJhSoQmAXQB89RgAd0cgA3MHNoAH1bYk59AG5FQJCwzEjgaDoqAHc4AAoASgTFYEIIU3QAV2BTPHRc6Nj8vxFUUwALcggAOii7DOhohMY1RlCQCswCzlgXN2ovJ31PZpasUwr0Amh2zp7gLrmIJx3u3uJPIbQVNRKCMowWfuzoHVlc1iZC9VLTDDEnzA5V7vCSfBJAA)\r\n\r\n**Related Issues:** \r\n- #36852\r\n- #20602\r\n","author":{"url":"https://github.com/rsesek","@type":"Person","name":"rsesek"},"datePublished":"2020-03-09T13:55:48Z","url":"https://github.com/microsoft/TypeScript/issues/37292"}
| 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:187c5a67-0950-59a4-a1c7-ebde7dd6752c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A298:17FB7:1E0A928:2611719:6B019C05 |
| html-safe-nonce | 0ef047682ee1b0b13461449f72c61f2f7dbe0fa0abef54fe8e6bb3b6de89154e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMjk4OjE3RkI3OjFFMEE5Mjg6MjYxMTcxOTo2QjAxOUMwNSIsInZpc2l0b3JfaWQiOiI3OTU1OTMxNTA2MDQyNTc1ODc3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2f4cfdeb8250e45f1b80efc876a861e3f276d89c6fc5f1083db30b932ed8564c |
| hovercard-subject-tag | issue:577927324 |
| 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/37292/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2ed1554e85612e7c8ca465ef28fa64d139ef691b808cd57664e1c9ea5a76e81b/microsoft/TypeScript/issues/37292 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2ed1554e85612e7c8ca465ef28fa64d139ef691b808cd57664e1c9ea5a76e81b/microsoft/TypeScript/issues/37292 |
| og:image:alt | TypeScript Version: 3.8.3 and 3.9.0-dev.20200309 Search Terms: [keyof quote], [keyof number declaration], [key number], [key quote], [object key number declaration] Code // test.ts class C { readon... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rsesek |
| 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