Title: Type checking does not works for indexable type with number keys OR string keys · Issue #34611 · microsoft/TypeScript · GitHub
Open Graph Title: Type checking does not works for indexable type with number keys OR string keys · Issue #34611 · microsoft/TypeScript
X Title: Type checking does not works for indexable type with number keys OR string keys · Issue #34611 · microsoft/TypeScript
Description: Try with newest TypeScript TypeScript Version: 3.7.0-dev.20191018 Result: has not been fixed. Search Terms: typescript indexable type string keys or number keys Code enum PROPERTIES_TYPES { NUMBER = "NUMBER", STRING = "STRING" // ETC. } ...
Open Graph Description: Try with newest TypeScript TypeScript Version: 3.7.0-dev.20191018 Result: has not been fixed. Search Terms: typescript indexable type string keys or number keys Code enum PROPERTIES_TYPES { NUMBER ...
X Description: Try with newest TypeScript TypeScript Version: 3.7.0-dev.20191018 Result: has not been fixed. Search Terms: typescript indexable type string keys or number keys Code enum PROPERTIES_TYPES { NUMBER ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/34611
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type checking does not works for indexable type with number keys OR string keys","articleBody":"**Try with newest TypeScript**\r\n**TypeScript Version:** 3.7.0-dev.20191018\r\nResult: has not been fixed.\r\n\r\n**Search Terms:**\r\n`typescript indexable type string keys or number keys`\r\n\r\n**Code**\r\n```ts\r\nenum PROPERTIES_TYPES {\r\n NUMBER = \"NUMBER\",\r\n STRING = \"STRING\"\r\n // ETC.\r\n}\r\n\r\ninterface IStringPropertySpecification {\r\n type: PROPERTIES_TYPES.STRING;\r\n required: boolean;\r\n nullable?: boolean;\r\n newName?: string;\r\n emptyStringIsAllowed: boolean;\r\n allowedValues?: Array\u003cstring>;\r\n invalidValueSubstitution: string;\r\n defaultValue?: string;\r\n}\r\n\r\ninterface IObjectDataSpecification__StringKeys {\r\n nameForLogging: string;\r\n properties: {\r\n [propertyName: string]: IStringPropertySpecification;\r\n };\r\n}\r\n\r\ninterface IObjectDataSpecification__NumberKeys {\r\n nameForLogging: string;\r\n properties: {\r\n [propertyName: number]: IStringPropertySpecification;\r\n };\r\n}\r\n\r\ntype ObjectDataSpecification = IObjectDataSpecification__StringKeys | IObjectDataSpecification__NumberKeys;\r\n\r\n\r\nconst dataSpecification: ObjectDataSpecification = {\r\n nameForLogging: \"Test\",\r\n properties: {\r\n foo: \"asdfsadffsd\" // Valid!!! No type errors!\r\n }\r\n};\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nThe error occurrence. I suppose, it must be:\r\n`TS2322 Type 'string' is not assignable to type ObjectDataSpecification`\r\n\r\n**Actual behavior:**\r\n\r\nTypeScript ignores this error. However, my PhpStorm knows right autocompletes: \r\n\r\n\r\n\r\n**Playground Link:** https://jsfiddle.net/pfg1mq96/\r\n\r\n**Why I need so strange type as ObjectDataSpecification?**\r\n\r\nThe `ObjectDataSpecification` has been created for external data validation. It's known in advance, which type external data SHOULD be, but it's unknown in advance, which data ACTUALLY WILL BE. If data satisfies `ObjectDataSpecification`, it will be cast as below:\r\n\r\n```\r\nconst validData: IValidData = rawData as IValidData;\r\n```\r\n\r\nIn above case, `IValidData` will be:\r\n\r\n```\r\ninterface IValidData { foo: string; }\r\n```\r\n\r\nNormally, programmers should not create type like:\r\n\r\n```\r\ntype ConfusingType {\r\n 1: string;\r\n foo: number;\r\n}\r\n```\r\n\r\nHowever I can't hope for high-quality API in the team work, The API team could define ANYTHING includes strange structure like above `ConfusingType` . Even it happens, I must to describe the `ConfusingType` with my `ObjectDataSpecification`. It will be:\r\n\r\n```\r\nconst dataSpecification: ObjectDataSpecification = {\r\n nameForLogging: \"ConfusingType \",\r\n properties: {\r\n 1: {\r\n type: PROPERTIES_TYPES.STRING,\r\n required: true,\r\n emptyStringIsAllowed: false,\r\n invalidValueSubstitution: \"---\"\r\n },\r\n foo: {\r\n type: PROPERTIES_TYPES.NUMBER,\r\n required: true,\r\n numberType: NUMBER_TYPES.NATURAL_NUMBER,\r\n invalidValueSubstitution: 1\r\n }\r\n }\r\n};\r\n```\r\n\r\nThen, my library validates external data according to `dataSpecification`.","author":{"url":"https://github.com/TokugawaTakeshi","@type":"Person","name":"TokugawaTakeshi"},"datePublished":"2019-10-21T08:16:50Z","url":"https://github.com/microsoft/TypeScript/issues/34611"}
| 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:114f6dab-0219-15cb-1f65-a725083a0661 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9E88:1F2D:2A1375:3544C2:6B00FF27 |
| html-safe-nonce | 392e51c1bf458bf3c17da58eb643177b49708c9fdb8f94b30620e923cf4cffb5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RTg4OjFGMkQ6MkExMzc1OjM1NDRDMjo2QjAwRkYyNyIsInZpc2l0b3JfaWQiOiIxODE0NjE3NTY4MzIxNzk3OTI3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 3d005b1eae3752975e20295ae5f3780611c5e0e3beeabfe5ae16998010dbeb6e |
| hovercard-subject-tag | issue:509804080 |
| 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/34611/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f1fdaaaba902b8ee2e7a271cf1a4d8a1f8ccfa42065089fefe42884b51c5b020/microsoft/TypeScript/issues/34611 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f1fdaaaba902b8ee2e7a271cf1a4d8a1f8ccfa42065089fefe42884b51c5b020/microsoft/TypeScript/issues/34611 |
| og:image:alt | Try with newest TypeScript TypeScript Version: 3.7.0-dev.20191018 Result: has not been fixed. Search Terms: typescript indexable type string keys or number keys Code enum PROPERTIES_TYPES { NUMBER ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | TokugawaTakeshi |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width