Title: TypeScript doesn't respect declared function argument type definition · Issue #51487 · microsoft/TypeScript · GitHub
Open Graph Title: TypeScript doesn't respect declared function argument type definition · Issue #51487 · microsoft/TypeScript
X Title: TypeScript doesn't respect declared function argument type definition · Issue #51487 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms argument type inconsistent respect function definition 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about 4.8.4 ⏯ Playground Link Playgroun...
Open Graph Description: Bug Report 🔎 Search Terms argument type inconsistent respect function definition 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries...
X Description: Bug Report 🔎 Search Terms argument type inconsistent respect function definition 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for ent...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/51487
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"TypeScript doesn't respect declared function argument type definition","articleBody":"# Bug Report\r\n\r\n### 🔎 Search Terms\r\n\r\nargument type inconsistent respect function definition\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about 4.8.4\r\n\r\n### ⏯ Playground Link\r\n\r\n\u003c!--\r\n A link to a TypeScript Playground \"Share\" link which shows this behavior\r\n\r\n The TypeScript Workbench can be used for more complex setups, try\r\n https://www.typescriptlang.org/dev/bug-workbench/\r\n\r\n As a last resort, you can link to a repo, but these will be slower for us to investigate.\r\n-->\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?#code/MYGwhgzhAEBiD29oG8CwAoaXrHgOwgBcAnAV2EPmIAoBKFDbJ6QgCwEsIA6AIzGOgBeaAEYATAGYA3I2wBfWVj7EAXNDykAtjwCmxGegXoMAM1J4K7fNE4BlAA47g7E+x0ATADwAVAHzV2PHtSQjVzdx1XPA9oAB91UhAQOOhvWjVA4MIbGG8GTGxiHUJSYjxoampCAE9HeBMbIJDoAEJBYQAicMjAjw76ADIBiszmtuENJNpaAyNTc0trADd+djAeEB0AQQgt4gBzAKbCADV+AH41DW09ejQCrFwCbJNEAElnsAsdIXUdAHc4Ig6AZFDYGgEIA4nC43O4jlkzsRpvlmNgnhB4JsuCB4IcAAZInLQCCOZyuDxqAAkyFGp34cnxMzBTFe8A+RC+wB0vH4vzpSIMTCMTB0IAgP3uaMe+Ex2Nxhw6KwEnGg5lJMIp7n6YKMc3QZgshCs5WVaw22wgAGFZYQ6Kj0bboGyOYQuT8JgCgfAQRgwRjsgL+FdEsl4t0ojF4tddAJhBHeu4hdgXCMoWTYR4ESEkSipdKMVieQrqIS+aqNeS4dTacckYzmQ9mAB6Zsuz7fXlxxqI-hSaCt6CeAC0w+gAFFiMQqLqwWKJQ60YX5XjqEryzB1RmtTqm3q-egAz2c8GEkkUgnou4UjG9L9Lx4DK2zetNjs9ocg8iDC+LTsbc8vroEAA)\r\n\r\n### 💻 Code\r\n\r\n\u003c!-- Please post the relevant code sample here as well-->\r\n```ts\r\nclass Foo {\r\n constructor() {\r\n this.bar = 123;\r\n }\r\n bar: number;\r\n}\r\n\r\nfunction isSpecified\u003cT>(input: undefined | null | T): input is T {\r\n return ((typeof input !== \"undefined\") && (input !== null));\r\n}\r\n\r\nfunction variableAsArg(inputVar?: number) {\r\n const fooInstance = new Foo();\r\n\r\n if (isSpecified(inputVar)) {\r\n console.log(`Var is specified: ${inputVar}`);\r\n fooInstance.bar = inputVar;\r\n }\r\n else {\r\n console.log(\"var is unspecified\")\r\n }\r\n}\r\n\r\nfunction variableAsConst() {\r\n const fooInstance = new Foo();\r\n\r\n const inputVar: null | undefined | number = undefined;\r\n if (isSpecified(inputVar)) {\r\n console.log(`Var is specified: ${inputVar}`);\r\n fooInstance.bar = inputVar; // \u003c-- Error\r\n }\r\n else {\r\n console.log(\"var is unspecified\")\r\n }\r\n}\r\n\r\nconst inputVar: null | undefined | number = undefined;\r\n//variableAsArg(inputVar);\r\nvariableAsConst();\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nWhen a variable is declared in the same block and assigned `undefined`, even when its type is explicitly declared to have ` | number`, its type is treated as `null | undefined` rather than `number | null | undefined` when passed to a function. I guess TS thinks it's being helpful here, but it seems to me like annoying inconsistent behaviour that breaks my type guard function when I've explicitly told TS that the variable type should be considered ` | number`. Because of this behaviour, in that circumstance, my `isSpecified` function no longer acts as a type guard against `null | undefined` and I get an error from TS.\r\n\r\n### 🙂 Expected behavior\r\n\r\nTS should always respect the explicit type that the variable is declared to be whether it's a function argument or declared in the same code block as a `const`.\r\n","author":{"url":"https://github.com/jez9999","@type":"Person","name":"jez9999"},"datePublished":"2022-11-11T13:14:33Z","url":"https://github.com/microsoft/TypeScript/issues/51487"}
| 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:89e5b703-e50a-e8d3-852c-c6a9df003dd8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D57E:BB75:7BED11:A39439:6B01AE0E |
| html-safe-nonce | 71208fd1a027db7ab5c1c8ae13db719bb0109621ea4bdca731652263659c7e8e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTdFOkJCNzU6N0JFRDExOkEzOTQzOTo2QjAxQUUwRSIsInZpc2l0b3JfaWQiOiI3NDU3NDIyMjQ1NDk4MDM5ODIyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1ea50c578443886a257441ec857d41f4bee7861c01957165d241f64734806a77 |
| hovercard-subject-tag | issue:1445458802 |
| 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/51487/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fef66eb3aa558f0035b9d95a506ff3b53224ad686298813f0ef3711669854baf/microsoft/TypeScript/issues/51487 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fef66eb3aa558f0035b9d95a506ff3b53224ad686298813f0ef3711669854baf/microsoft/TypeScript/issues/51487 |
| og:image:alt | Bug Report 🔎 Search Terms argument type inconsistent respect function definition 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jez9999 |
| 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