Title: Way to specify that type can't be null · Issue #14967 · microsoft/TypeScript · GitHub
Open Graph Title: Way to specify that type can't be null · Issue #14967 · microsoft/TypeScript
X Title: Way to specify that type can't be null · Issue #14967 · microsoft/TypeScript
Description: TypeScript Version: 2.2.1 Code With the code function filterMap
Open Graph Description: TypeScript Version: 2.2.1 Code With the code function filterMap
X Description: TypeScript Version: 2.2.1 Code With the code function filterMap<T, U>(data: Array<T>, fn: (element: T) => U): Array<U> { let newArray: Array<U> = [] data.forEach(element ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/14967
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Way to specify that type can't be null","articleBody":"\u003c!-- BUGS: Please use this template. -->\r\n\u003c!-- QUESTIONS: This is not a general support forum! Ask Qs at http://stackoverflow.com/questions/tagged/typescript -->\r\n\u003c!-- SUGGESTIONS: See https://github.com/Microsoft/TypeScript-wiki/blob/master/Writing-Good-Design-Proposals.md -->\r\n\r\n**TypeScript Version:** 2.2.1\r\n\r\n**Code**\r\n\r\nWith the code\r\n```ts\r\nfunction filterMap\u003cT, U>(data: Array\u003cT>, fn: (element: T) => U): Array\u003cU> {\r\n let newArray: Array\u003cU> = []\r\n data.forEach(element => {\r\n let newElement = fn(element)\r\n if (newElement != null) {\r\n newArray.push(newElement)\r\n }\r\n })\r\n return newArray\r\n}\r\n\r\nlet fruits = ['apple', 'banana', 'pear']\r\nlet filteredAndMapped = filterMap(fruits, e => e.startsWith('a') ? null : e.toUpperCase())\r\n```\r\n`filteredAndMapped` now have the type `Array\u003cstring | null>` but in reality only contain elements of type string.\r\n\r\nIt would be nice if it was possible to do a non-null assertion or similar for types, allowing me to write something similar to\r\n```\r\nfunction filterMap\u003cT, U>(data: Array\u003cT>, fn: (element: T) => U): Array\u003cU!> {\r\n let newArray: Array\u003cU!> = []\r\n // ...\r\n```\r\nso that `filteredAndMapped` and `newArray` have the type `Array\u003cstring>`.","author":{"url":"https://github.com/Pajn","@type":"Person","name":"Pajn"},"datePublished":"2017-04-01T12:44:14Z","url":"https://github.com/microsoft/TypeScript/issues/14967"}
| 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:0f35a95c-fc40-9b9a-f828-d7e357e92ee5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B2BA:DB09F:4DCEFA:64EC31:6B00D9B5 |
| html-safe-nonce | fab4c3259ad0fe9bbfc3ade60e0621e0a7338b00d77e1d3860c66610e39f64b4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMkJBOkRCMDlGOjREQ0VGQTo2NEVDMzE6NkIwMEQ5QjUiLCJ2aXNpdG9yX2lkIjoiMjczNDIzNDEzNDQyMjU0Mjc3NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 102cb4cb748bd05254c50192b399d16265c3cce959f69a5dfb5b2d146125ae19 |
| hovercard-subject-tag | issue:218693509 |
| 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/14967/issue_layout |
| twitter:image | https://opengraph.githubassets.com/681abc640e559e740f5d45a5aa7b1a5dc0e2e1059cb4857858e3b50f73b57023/microsoft/TypeScript/issues/14967 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/681abc640e559e740f5d45a5aa7b1a5dc0e2e1059cb4857858e3b50f73b57023/microsoft/TypeScript/issues/14967 |
| og:image:alt | TypeScript Version: 2.2.1 Code With the code function filterMap |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Pajn |
| hostname | github.com |
| expected-hostname | github.com |
| None | a481f62a1e04c9b9bdcb5628230682c66d59006f5b13b080f79cba84a176905b |
| 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 | 84c8c5cc25e2fb7f2308428458538436baecc6d2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width