Title: Too-permissive assignability with complex discriminated unions · Issue #34751 · microsoft/TypeScript · GitHub
Open Graph Title: Too-permissive assignability with complex discriminated unions · Issue #34751 · microsoft/TypeScript
X Title: Too-permissive assignability with complex discriminated unions · Issue #34751 · microsoft/TypeScript
Description: I was trying to add type safety to some model i have and it seems to sometimes work in a weird way. Code export enum FilterType { String = 'string', Number = 'number', Boolean = 'boolean' } export interface FilterTypeToLiteralTypeMap { [...
Open Graph Description: I was trying to add type safety to some model i have and it seems to sometimes work in a weird way. Code export enum FilterType { String = 'string', Number = 'number', Boolean = 'boolean' } export ...
X Description: I was trying to add type safety to some model i have and it seems to sometimes work in a weird way. Code export enum FilterType { String = 'string', Number = 'number', Boolean = ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/34751
X: @github
Domain: github.com
IPv4 address: 140.82.113.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Too-permissive assignability with complex discriminated unions","articleBody":"I was trying to add type safety to some model i have and it seems to sometimes work in a weird way.\r\n\r\n**Code**\r\n\r\n```ts\r\nexport enum FilterType {\r\n String = 'string',\r\n Number = 'number',\r\n Boolean = 'boolean'\r\n}\r\n\r\nexport interface FilterTypeToLiteralTypeMap {\r\n [FilterType.String]: string;\r\n [FilterType.Number]: number;\r\n [FilterType.Boolean]: boolean;\r\n}\r\n\r\nexport type AllowedFilterTypeLiteralTypes = FilterTypeToLiteralTypeMap[FilterType];\r\n\r\nexport type Values\u003cT> = T[keyof T];\r\n\r\nexport type FilterTypeFromLiteralType\u003c\r\n T extends AllowedFilterTypeLiteralTypes\r\n> = Exclude\u003c\r\n Values\u003c\r\n {\r\n [key in FilterType]: FilterTypeToLiteralTypeMap[key] extends T\r\n ? key\r\n : never;\r\n }\r\n >,\r\n never\r\n>;\r\n\r\nexport interface Filterable\u003cT extends AllowedFilterTypeLiteralTypes> {\r\n isFilterable: true;\r\n filterType: FilterTypeFromLiteralType\u003cT>;\r\n}\r\n\r\nexport interface NotFilterable {\r\n isFilterable: false;\r\n}\r\n\r\nexport type FilterableTrait\u003cT extends AllowedFilterTypeLiteralTypes> =\r\n | Filterable\u003cT>\r\n | NotFilterable;\r\n```\r\n\r\nWhat i did here is created an enum and mapped each entry to a literal type. That way when using the `FilterableTrait` interface it can be either `Filterable` or `NotFilterable` and if filterable the `FilterType` is decided based on the given type.\r\n\r\nSome usages that work\r\n\r\n```ts\r\n// Errors correctly\r\nexport const a: FilterableTrait\u003cstring> = {\r\n isFilterable: true,\r\n filterType: FilterType.Number\r\n};\r\n\r\n// Works correctly\r\nexport const b: FilterableTrait\u003cstring> = {\r\n isFilterable: true,\r\n filterType: FilterType.String\r\n};\r\n\r\nexport const c: FilterableTrait\u003cstring> = {\r\n isFilterable: false,\r\n filterType: FilterType.String // Errors correctly\r\n};\r\n\r\n// Errors correctly\r\nexport const d: FilterableTrait\u003cstring> | FilterableTrait\u003cnumber> = {\r\n isFilterable: true,\r\n filterType: FilterType.Boolean\r\n};\r\n\r\n// Works correctly\r\nexport const e: FilterableTrait\u003cstring> | FilterableTrait\u003cnumber> = {\r\n isFilterable: true,\r\n filterType: FilterType.Number\r\n};\r\n```\r\n\r\nThe problem starts when i do this\r\n```ts\r\nexport const f: FilterableTrait\u003cstring> | FilterableTrait\u003cnumber> = {\r\n isFilterable: false,\r\n filterType: FilterType.Number // No error? Auto complete doesn't add this as an option but it compiles\r\n};\r\n```\r\n\r\nA few notes here:\r\n1) If i change `Filterable` to be `filterType: FilterTypeFromLiteralType\u003cstring>` instead of `filterType: FilterTypeFromLiteralType\u003cT>` it works... it doesnt work just when i use the generic type.\r\n2) I know i can use\r\n```ts\r\nexport const g: FilterableTrait\u003cstring | number> = {\r\n isFilterable: false,\r\n filterType: FilterType.Number\r\n};\r\n```\r\n\r\nand it will work but my actual use case requires me to use something like\r\n```ts\r\nexport const h:\r\n | ({ type: '1' } & FilterableTrait\u003cstring>)\r\n | ({ type: '2' } & FilterableTrait\u003cnumber>) = {\r\n type: '2',\r\n isFilterable: true,\r\n filterType: FilterType.Number\r\n};\r\n```\r\n\r\nWhich means i can't use the above\r\n\r\n**Expected behavior:**\r\nCompilation error\r\n\r\n**Actual behavior:**\r\nCompiles successfully\r\n\r\n**Playground Link:** http://www.typescriptlang.org/play/?ts=3.7-Beta&ssl=31&ssc=3&pln=17&pc=1#","author":{"url":"https://github.com/Harpush","@type":"Person","name":"Harpush"},"datePublished":"2019-10-26T11:16:21Z","url":"https://github.com/microsoft/TypeScript/issues/34751"}
| 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:b320f4d4-935f-d18e-49e5-acda0863d508 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D4AA:1DFD99:8D21B7:B92340:6B04CFD3 |
| html-safe-nonce | c1857441be692d31f2b3ba57506d0984ad4e41b92fdc6ced767b2d49c05a94b7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENEFBOjFERkQ5OTo4RDIxQjc6QjkyMzQwOjZCMDRDRkQzIiwidmlzaXRvcl9pZCI6IjQwNjcwMTcxNDg0MjAzNzg1NzkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8a2634eeef714ceb5442e4bbb9bd691ce58aa409454eed7911632e98efa3aa16 |
| hovercard-subject-tag | issue:512823544 |
| 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/34751/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7b1bcc4ec4cfb10deb781dd901942485c8a6435080d9b6d5abe4503bcde34862/microsoft/TypeScript/issues/34751 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7b1bcc4ec4cfb10deb781dd901942485c8a6435080d9b6d5abe4503bcde34862/microsoft/TypeScript/issues/34751 |
| og:image:alt | I was trying to add type safety to some model i have and it seems to sometimes work in a weird way. Code export enum FilterType { String = 'string', Number = 'number', Boolean = 'boolean' } export ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Harpush |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27ed421e5c2dac040a37529b5b9191edcc4a3de70521471e81fb4b4d9dc2aa7d |
| 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 | 63dc19014c621c67306d7899c04c78dcd0d95803 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width