Title: [Feature Request] Built in to make a type non-nullable · Issue #1874 · AssemblyScript/assemblyscript · GitHub
Open Graph Title: [Feature Request] Built in to make a type non-nullable · Issue #1874 · AssemblyScript/assemblyscript
X Title: [Feature Request] Built in to make a type non-nullable · Issue #1874 · AssemblyScript/assemblyscript
Description: Consider the following example: function takesGeneric
Open Graph Description: Consider the following example: function takesGeneric
X Description: Consider the following example: function takesGeneric<T>(t: T): void { if (!isNullable<T>() || t != null){ // @ts-ignore if (isDefined(t.call)){ t.call() } } } function callsGeneric<...
Opengraph URL: https://github.com/AssemblyScript/assemblyscript/issues/1874
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Feature Request] Built in to make a type non-nullable","articleBody":"[Consider the following example:](https://www.assemblyscript.org/editor.html#IyFvcHRpbWl6ZT1zcGVlZCZydW50aW1lPXN0dWIKLyoqIENhbGN1bGF0ZXMgdGhlIG4tdGggRmlib25hY2NpIG51bWJlci4gKi8KZXhwb3J0IGZ1bmN0aW9uIGZpYihuOiBpMzIpOiBpMzIgewogIHZhciBhID0gMCwgYiA9IDEKICBpZiAobiA+IDApIHsKICAgIHdoaWxlICgtLW4pIHsKICAgICAgbGV0IHQgPSBhICsgYgogICAgICBhID0gYgogICAgICBiID0gdAogICAgfQogICAgcmV0dXJuIGIKICB9CiAgcmV0dXJuIGEKfQoKZnVuY3Rpb24gdGFrZXNHZW5lcmljPFQ+KHQ6IFQpOiB2b2lkIHsKICBpZiAoIWlzTnVsbGFibGU8VD4oKSB8fCB0ICE9IG51bGwpewogICAgICAvLyBAdHMtaWdub3JlCiAgICAgIGlmICAoaXNEZWZpbmVkKHQuY2FsbCkpewogICAgICAgIHQuY2FsbCgpCiAgICAgIH0KICAgIH0KfQoKZnVuY3Rpb24gY2FsbHNHZW5lcmljPFQ+KHQ6IFQpOiB2b2lkIHsKICBpZiAodCAhPSBudWxsKSB7CiAgICB0YWtlc0dlbmVyaWModCk7CiAgfQp9CgpjbGFzcyBUZWUgewogIGNhbGwoKTogdm9pZCB7CiAgICBsZXQgeDogaTggPSAwOwogIH0KfQoKbGV0IHk6IFRlZSB8IG51bGwgPSBuZXcgVGVlKCk7CgpjYWxsc0dlbmVyaWMoeSk7CiMhaHRtbAo8dGV4dGFyZWEgaWQ9Im91dHB1dCIgc3R5bGU9ImhlaWdodDogMTAwJTsgd2lkdGg6IDEwMCUiIHJlYWRvbmx5PjwvdGV4dGFyZWE+CjxzY3JpcHQ+CmxvYWRlci5pbnN0YW50aWF0ZShtb2R1bGVfd2FzbSwgeyAvKiBpbXBvcnRzICovIH0pCiAgLnRoZW4oKHsgZXhwb3J0cyB9KSA9PiB7CiAgICBjb25zdCBvdXRwdXQgPSBkb2N1bWVudC5nZXRFbGVtZW50QnlJZCgnb3V0cHV0JykKICAgIGZvciAobGV0IGkgPSAwOyBpIDw9IDEwOyArK2kpIHsKICAgICAgb3V0cHV0LnZhbHVlICs9IGBmaWIoJHtpfSkgPSAke2V4cG9ydHMuZmliKGkpfVxuYAogICAgfQogIH0pCjwvc2NyaXB0Pg==)\r\n\r\n```ts\r\n\r\nfunction takesGeneric\u003cT>(t: T): void {\r\n if (!isNullable\u003cT>() || t != null){\r\n // @ts-ignore\r\n if (isDefined(t.call)){\r\n t.call()\r\n }\r\n }\r\n}\r\n\r\nfunction callsGeneric\u003cT>(t: T): void {\r\n if (t != null) {\r\n takesGeneric(t); // Would be nice to have a `notNull\u003cT>` here\r\n }\r\n}\r\n\r\nclass Tee {\r\n call(): void {\r\n let x: i8 = 0;\r\n }\r\n}\r\n\r\nlet y: Tee | null = new Tee();\r\n\r\ncallsGeneric(y);\r\n```\r\n\r\nThe type passed to `takesGeneric` is no longer nullable since it's guaranteed to not be null. However, the type passed doesn't reflect this. A quick fix to this could be to add a new builtin that converts a `type | null` to `type`. Or in branches where the type is non-null this happens automatically.","author":{"url":"https://github.com/willemneal","@type":"Person","name":"willemneal"},"datePublished":"2021-05-28T17:57:40Z","url":"https://github.com/AssemblyScript/assemblyscript/issues/1874"}
| 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:6509d818-5af8-ba84-4446-7498d9f1184f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8928:21149E:DDB596:12F670D:6B00B4F8 |
| html-safe-nonce | 7b4a523be6d2a876495550f903e933efd4270b984fd38babeb4616315bc9ce80 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OTI4OjIxMTQ5RTpEREI1OTY6MTJGNjcwRDo2QjAwQjRGOCIsInZpc2l0b3JfaWQiOiIyNzE2NTc2NTY2MDkwOTcwMzYwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | d96ed19d4467839774424edc89e07ff7f6df80ff05280feda039430e541290dd |
| hovercard-subject-tag | issue:905821484 |
| 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/AssemblyScript/assemblyscript/1874/issue_layout |
| twitter:image | https://opengraph.githubassets.com/aba3bb567bea4a2a443b625ca5823a4590639432a69d32c468308f5d1e590b15/AssemblyScript/assemblyscript/issues/1874 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/aba3bb567bea4a2a443b625ca5823a4590639432a69d32c468308f5d1e590b15/AssemblyScript/assemblyscript/issues/1874 |
| og:image:alt | Consider the following example: function takesGeneric |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | willemneal |
| hostname | github.com |
| expected-hostname | github.com |
| None | 1d07c25977adfc5ff6acb0a962a9bc168c07afbda6fef765f9761c350c2ae4a2 |
| turbo-cache-control | no-preview |
| go-import | github.com/AssemblyScript/assemblyscript git https://github.com/AssemblyScript/assemblyscript.git |
| octolytics-dimension-user_id | 28916798 |
| octolytics-dimension-user_login | AssemblyScript |
| octolytics-dimension-repository_id | 105138356 |
| octolytics-dimension-repository_nwo | AssemblyScript/assemblyscript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 105138356 |
| octolytics-dimension-repository_network_root_nwo | AssemblyScript/assemblyscript |
| 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 | 5429cbfc6aca8c7b9a086a3c8f47400346f38cc9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width