Title: Any number matches a numeric enum · Issue #21546 · microsoft/TypeScript · GitHub
Open Graph Title: Any number matches a numeric enum · Issue #21546 · microsoft/TypeScript
X Title: Any number matches a numeric enum · Issue #21546 · microsoft/TypeScript
Description: TypeScript Version: 2.7.1 Search Terms: enum numeric number type Code enum TestStringEnum { a = "a", b = "b", } function testStringEnum(test: TestStringEnum) { return test; } testStringEnum(9); // fails (expected) testStringEnum("9"); //...
Open Graph Description: TypeScript Version: 2.7.1 Search Terms: enum numeric number type Code enum TestStringEnum { a = "a", b = "b", } function testStringEnum(test: TestStringEnum) { return test; } testStringEnum(9); // ...
X Description: TypeScript Version: 2.7.1 Search Terms: enum numeric number type Code enum TestStringEnum { a = "a", b = "b", } function testStringEnum(test: TestStringEnum) { return test; } te...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/21546
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Any number matches a numeric enum","articleBody":"\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 2.7.1\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** enum numeric number type \r\n\r\n**Code**\r\n\r\n```ts\r\nenum TestStringEnum {\r\n a = \"a\",\r\n b = \"b\",\r\n}\r\n\r\nfunction testStringEnum(test: TestStringEnum) {\r\n return test;\r\n}\r\n\r\ntestStringEnum(9); // fails (expected)\r\ntestStringEnum(\"9\"); // fails (expected)\r\ntestStringEnum(TestStringEnum.a); // passes (expected)\r\ntestStringEnum(\"a\"); // fails (???)\r\n\r\ntype TestNumberUnion = 4 | 5;\r\n\r\nfunction testNumberUnion(test: TestNumberUnion) {\r\n return test;\r\n}\r\n\r\ntestNumberUnion(9); // fails (expected)\r\ntestNumberUnion(\"9\"); // fails (expected)\r\ntestNumberUnion(4); // passes (expected)\r\n\r\nenum TestNumberEnum {\r\n a = 1,\r\n b = 2,\r\n}\r\n\r\nfunction testNumberEnum(test: TestNumberEnum) {\r\n return test;\r\n}\r\n\r\ntestNumberEnum(9); // passes (not expected)\r\ntestNumberEnum(\"9\"); // fails (expected)\r\ntestNumberEnum(TestNumberEnum.a); // passes (expected)\r\ntestNumberEnum(1); // passes (???)\r\n\r\n```\r\n\r\n**Expected behavior:**\r\nThe behavior of a string backed enum should match a number backed enum.\r\n\r\n**Actual behavior:**\r\nA number backed enum is treated as equivalent to any number.\r\n\r\n**Playground Link:** [playground link](https://www.typescriptlang.org/play/#src=enum%20TestStringEnum%20%7B%0D%0A%20%20%20%20a%20%3D%20%22a%22%2C%0D%0A%20%20%20%20b%20%3D%20%22b%22%2C%0D%0A%7D%0D%0A%0D%0Afunction%20testStringEnum(test%3A%20TestStringEnum)%20%7B%0D%0A%20%20%20%20return%20test%3B%0D%0A%7D%0D%0A%0D%0AtestStringEnum(9)%3B%20%2F%2F%20fails%20(expected)%0D%0AtestStringEnum(%229%22)%3B%20%2F%2F%20fails%20(expected)%0D%0AtestStringEnum(TestStringEnum.a)%3B%20%2F%2F%20passes%20(expected)%0D%0AtestStringEnum(%22a%22)%3B%20%2F%2F%20fails%20(%3F%3F%3F)%0D%0A%0D%0Atype%20TestNumberUnion%20%3D%204%20%7C%205%3B%0D%0A%0D%0Afunction%20testNumberUnion(test%3A%20TestNumberUnion)%20%7B%0D%0A%20%20%20%20return%20test%3B%0D%0A%7D%0D%0A%0D%0AtestNumberUnion(9)%3B%20%2F%2F%20fails%20(expected)%0D%0AtestNumberUnion(%229%22)%3B%20%2F%2F%20fails%20(expected)%0D%0AtestNumberUnion(4)%3B%20%2F%2F%20passes%20(expected)%0D%0A%0D%0Aenum%20TestNumberEnum%20%7B%0D%0A%20%20%20%20a%20%3D%201%2C%0D%0A%20%20%20%20b%20%3D%202%2C%0D%0A%7D%0D%0A%0D%0Afunction%20testNumberEnum(test%3A%20TestNumberEnum)%20%7B%0D%0A%20%20%20%20return%20test%3B%0D%0A%7D%0D%0A%0D%0AtestNumberEnum(9)%3B%20%2F%2F%20passes%20(not%20expected)%0D%0AtestNumberEnum(%229%22)%3B%20%2F%2F%20fails%20(expected)%0D%0AtestNumberEnum(TestNumberEnum.a)%3B%20%2F%2F%20passes%20(expected)%0D%0AtestNumberEnum(1)%3B%20%2F%2F%20passes%20(%3F%3F%3F))\r\n\r\n**Related Issues:**\r\n","author":{"url":"https://github.com/apexskier","@type":"Person","name":"apexskier"},"datePublished":"2018-02-01T20:15:28Z","url":"https://github.com/microsoft/TypeScript/issues/21546"}
| 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:f3b94a34-0362-dc99-5f63-1f8bac7afb2d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BCD0:4279A:222419A:2DF0B1C:6B019E8A |
| html-safe-nonce | 50fe33a9d44b12d337e1567db02c6439c67b787589ed74332617103853eda708 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQ0QwOjQyNzlBOjIyMjQxOUE6MkRGMEIxQzo2QjAxOUU4QSIsInZpc2l0b3JfaWQiOiI4NzIzNzk2ODE0NDY3Mjc2NDI2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | b4d78d2bf10ab7d4efd6057e24cf2f1a9db34f2aaf6d1d5be5bc3f52a2c62fd9 |
| hovercard-subject-tag | issue:293668776 |
| 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/21546/issue_layout |
| twitter:image | https://opengraph.githubassets.com/33bac029c614967432223b81199a3e588cee4193b03f796c9cc2ebf31b17558d/microsoft/TypeScript/issues/21546 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/33bac029c614967432223b81199a3e588cee4193b03f796c9cc2ebf31b17558d/microsoft/TypeScript/issues/21546 |
| og:image:alt | TypeScript Version: 2.7.1 Search Terms: enum numeric number type Code enum TestStringEnum { a = "a", b = "b", } function testStringEnum(test: TestStringEnum) { return test; } testStringEnum(9); // ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | apexskier |
| 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