Title: Proposal: Run-time Type Checks · Issue #7607 · microsoft/TypeScript · GitHub
Open Graph Title: Proposal: Run-time Type Checks · Issue #7607 · microsoft/TypeScript
X Title: Proposal: Run-time Type Checks · Issue #7607 · microsoft/TypeScript
Description: Hi there! I am writing a lot of code in an environment where JavaScript and TypeScript freely interact with each other. One thing that we continue to run into is where we would write a function in TypeScript, say: export function add(x: ...
Open Graph Description: Hi there! I am writing a lot of code in an environment where JavaScript and TypeScript freely interact with each other. One thing that we continue to run into is where we would write a function in ...
X Description: Hi there! I am writing a lot of code in an environment where JavaScript and TypeScript freely interact with each other. One thing that we continue to run into is where we would write a function in ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/7607
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal: Run-time Type Checks","articleBody":"Hi there!\n\nI am writing a lot of code in an environment where JavaScript and TypeScript freely interact with each other. One thing that we continue to run into is where we would write a function in TypeScript, say:\n\n```\nexport function add(x: number, y: number): number {\n return x + y;\n}\n```\n\nThis function is callable from JavaScript, where we're allowed to do things like:\n\n```\nadd('abc', undefined)\n```\n\nThis has lead us to introduce type checks for all exported functions in TypeScript, although they really feel verbose in a typed language.\n\n```\nexport function add(x: number, y: number): number {\n if (typeof x !== 'number' || typeof y !== 'number') {\n throw new TypeError('x and y need to be numbers');\n }\n return x + y;\n}\n```\n\nMy idea is to provide an optional flag, say `--insertRuntimeTypeChecks`, that makes the compiler insert these kinds of checks for function arguments.\n\nThe feature will probably be a bit more complicated when thinking about structural types, etc., especially in interplay with classes (it is my understanding that classes are type-checked structurally, not nominally, is that correct?).\n\nBut even having it only for primitive types (checking for `object`, `string`, `number`, etc.) could provide a lot of benefits for those of us who write code that interacts with JavaScript, especially should #7140 land, to spare us from manually writing `if (x === null) ...`.\n\nI'd be interested in giving implementing this a shot, if there's interest from the maintainers.\n","author":{"url":"https://github.com/srijs","@type":"Person","name":"srijs"},"datePublished":"2016-03-21T07:33:17Z","url":"https://github.com/microsoft/TypeScript/issues/7607"}
| 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:6ae39ed8-6889-20b6-caff-d5e0c3e54d19 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 88AA:2454CC:19DEB44:20CD95F:6B014B18 |
| html-safe-nonce | ef1daf58c7a5549efac58532378e5997f5603cb8ce3a14fbc764329047e98d42 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OEFBOjI0NTRDQzoxOURFQjQ0OjIwQ0Q5NUY6NkIwMTRCMTgiLCJ2aXNpdG9yX2lkIjoiMjM0NDA2NTIwMDU4MTMzMTczNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2401eab4518b3bec13a6655fc301860814e248784d527a6deda8e558f39551f2 |
| hovercard-subject-tag | issue:142276000 |
| 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/7607/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b226b2c5a0e46148671bfa119601bf748555ea21d04000825ae4ac8c99dac29c/microsoft/TypeScript/issues/7607 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b226b2c5a0e46148671bfa119601bf748555ea21d04000825ae4ac8c99dac29c/microsoft/TypeScript/issues/7607 |
| og:image:alt | Hi there! I am writing a lot of code in an environment where JavaScript and TypeScript freely interact with each other. One thing that we continue to run into is where we would write a function in ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | srijs |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width