Title: Tuple with conditional optional type · Issue #31409 · microsoft/TypeScript · GitHub
Open Graph Title: Tuple with conditional optional type · Issue #31409 · microsoft/TypeScript
X Title: Tuple with conditional optional type · Issue #31409 · microsoft/TypeScript
Description: Summary: I can define in typescript that tuple contains optional elements, e.g. let array: [string?]; - I can assign empty array. But can I combine this with conditional (generic) type: when type is defined then value should be set. Othe...
Open Graph Description: Summary: I can define in typescript that tuple contains optional elements, e.g. let array: [string?]; - I can assign empty array. But can I combine this with conditional (generic) type: when type i...
X Description: Summary: I can define in typescript that tuple contains optional elements, e.g. let array: [string?]; - I can assign empty array. But can I combine this with conditional (generic) type: when type i...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/31409
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Tuple with conditional optional type","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n**Summary:** \r\nI can define in typescript that tuple contains optional elements, e.g. `let array: [string?];` - I can assign empty array. But can I combine this with conditional (generic) type: when type is defined then value should be set. Otherwise, value is disallowed. Or at least optional if not disallowed.\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** \r\n3.5.0-dev.20190515\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\ntuple with conditional optional type \r\n\r\n**Code**\r\n\r\n```ts\r\ninterface A\u003cT = never> {\r\n array: [\r\n [T] extends [never] ? never : T, // how to make this `never` as optional?\r\n // smth like this does not work:\r\n // [T] extends [void] ? never? : T1, //error: `JSDoc types can only be used inside of documentation comments`\r\n // T? // optional works here correctly\r\n ]\r\n}\r\n\r\nlet test1: A\u003cstring>;\r\ntest1.array = [\"some string\"]\r\ntest1.array = [] // error: correctly\r\n\r\nlet test2: A;\r\ntest2.array = [ \"\" ] // error: correctly\r\ntest2.array = [ undefined ] // error: correctly\r\ntest2.array = [ null ] // error: correctly\r\ntest2.array = []; // error: INCORRECTLY! `Property '0' is missing in type '[]' but required in type '[never]'.`\r\n```\r\n\r\n**Expected behavior:**\r\nI can assign empty array to `test2.array`. Even more - I cannot assign not empty array.\r\n\r\n**Actual behavior:**\r\nI cannot assign empty array. I cannot assign also array with some value. I have to use some other type and pass it explicitly, e.g. undefined:\r\n```ts\r\ninterface A\u003cT = never> {\r\n array: [\r\n [T] extends [never] ? undefined : T\r\n ]\r\n}\r\n\r\nlet test1: A\u003cstring>;\r\ntest1.array = [\"some string\"]\r\ntest1.array = [] // error: correctly\r\n\r\nlet test2: A;\r\ntest2.array = [ \"\" ] // error: correctly\r\ntest2.array = [ undefined ] // error: correctly\r\ntest2.array = [ null ] // no error: is it correct?\r\ntest2.array = []; // error: INCORRECTLY! `Property '0' is missing in type '[]' but required in type '[never]'.`\r\n```\r\n\r\n[**Playground Link**](https://www.typescriptlang.org/play/#src=interface%20A%3CT%20%3D%20never%3E%20%7B%0D%0A%20%20%20%20array%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%5BT%5D%20extends%20%5Bnever%5D%20%3F%20never%20%3A%20T%2C%20%2F%2F%20how%20to%20make%20this%20%60never%60%20as%20optional%3F%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20smth%20like%20this%20does%20not%20work%3A%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20%5BT%5D%20extends%20%5Bvoid%5D%20%3F%20never%3F%20%3A%20T1%2C%20%2F%2Ferror%3A%20%60JSDoc%20types%20can%20only%20be%20used%20inside%20of%20documentation%20comments%60%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20T%3F%20%2F%2F%20%22plain%22%20optional%20works%20here%20correctly%0D%0A%20%20%20%20%5D%0D%0A%7D%0D%0A%0D%0Alet%20test1%3A%20A%3Cstring%3E%3B%0D%0Atest1.array%20%3D%20%5B%22some%20string%22%5D%0D%0Atest1.array%20%3D%20%5B%5D%20%2F%2F%20error%3A%20correctly%0D%0A%0D%0Alet%20test2%3A%20A%3B%0D%0Atest2.array%20%3D%20%5B%20%22%22%20%5D%20%2F%2F%20error%3A%20correctly%0D%0Atest2.array%20%3D%20%5B%20undefined%20%5D%20%2F%2F%20error%3A%20correctly%0D%0Atest2.array%20%3D%20%5B%20null%20%5D%20%2F%2F%20error%3A%20correctly%0D%0Atest2.array%20%3D%20%5B%5D%3B%20%2F%2F%20error%3A%20INCORRECTLY!%20%60Property%20'0'%20is%20missing%20in%20type%20'%5B%5D'%20but%20required%20in%20type%20'%5Bnever%5D'.%60)","author":{"url":"https://github.com/Krzyrok","@type":"Person","name":"Krzyrok"},"datePublished":"2019-05-15T15:13:45Z","url":"https://github.com/microsoft/TypeScript/issues/31409"}
| 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:45cdfec8-72e3-6aa9-d035-ba0cc5119a4f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E87E:395430:A8494B:E0154F:6B00E9FC |
| html-safe-nonce | 58b8e8f1c2c3767cd78c31be9a09e826742c686220966caa6651afa743e818f2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFODdFOjM5NTQzMDpBODQ5NEI6RTAxNTRGOjZCMDBFOUZDIiwidmlzaXRvcl9pZCI6IjY5MjcyNjE4ODQ4OTA1NDA1NDAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b470e0bf77c71be55e898d0b7c47d6c55ce722ec6e0e6714dfcde9f2812c8047 |
| hovercard-subject-tag | issue:444496613 |
| 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/31409/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5060282f975a326a2a9af88494058da213e91e1da4db4c2bb297a00045ad34fa/microsoft/TypeScript/issues/31409 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5060282f975a326a2a9af88494058da213e91e1da4db4c2bb297a00045ad34fa/microsoft/TypeScript/issues/31409 |
| og:image:alt | Summary: I can define in typescript that tuple contains optional elements, e.g. let array: [string?]; - I can assign empty array. But can I combine this with conditional (generic) type: when type i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Krzyrok |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width