Title: Typeguard for union not working after adding a property to the superclass of one of the union members · Issue #35953 · microsoft/TypeScript · GitHub
Open Graph Title: Typeguard for union not working after adding a property to the superclass of one of the union members · Issue #35953 · microsoft/TypeScript
X Title: Typeguard for union not working after adding a property to the superclass of one of the union members · Issue #35953 · microsoft/TypeScript
Description: This issue appeared in eslint-plugin-jest after updating @typescript-eslint to 2.13.0 (from 2.12.0). TypeScript Version: 3.7.x-dev.201xxxxx Search Terms: typeguard, union Code export declare enum AST_NODE_TYPES { Literal = 'Literal', Tem...
Open Graph Description: This issue appeared in eslint-plugin-jest after updating @typescript-eslint to 2.13.0 (from 2.12.0). TypeScript Version: 3.7.x-dev.201xxxxx Search Terms: typeguard, union Code export declare enum A...
X Description: This issue appeared in eslint-plugin-jest after updating @typescript-eslint to 2.13.0 (from 2.12.0). TypeScript Version: 3.7.x-dev.201xxxxx Search Terms: typeguard, union Code export declare enum A...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/35953
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Typeguard for union not working after adding a property to the superclass of one of the union members","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\nThis issue appeared in `eslint-plugin-jest` after updating `@typescript-eslint` to 2.13.0 (from 2.12.0).\r\n\r\n\r\n\u003c!--\r\nPlease try to reproduce the issue with the latest published version. It may have already been fixed.\r\nFor npm: `typescript@next`\r\nThis is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly\r\n-->\r\n**TypeScript Version:** 3.7.x-dev.201xxxxx\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:**\r\ntypeguard, union\r\n**Code**\r\n\r\n```ts\r\nexport declare enum AST_NODE_TYPES {\r\n Literal = 'Literal',\r\n TemplateLiteral = 'TemplateLiteral',\r\n}\r\n\r\nexport type TSNode = StringLiteral | TemplateLiteral;\r\n\r\nexport interface BaseNode {\r\n stuff: null;\r\n}\r\nexport interface LiteralBase extends BaseNode {\r\n raw: string;\r\n value: boolean | number | RegExp | string | null;\r\n regex?: {\r\n pattern: string;\r\n flags: string;\r\n };\r\n}\r\n\r\nexport interface StringLiteral extends LiteralBase {\r\n type: AST_NODE_TYPES.Literal;\r\n value: string;\r\n}\r\nexport interface TemplateLiteral extends BaseNode {\r\n type: AST_NODE_TYPES.TemplateLiteral;\r\n quasis: string[];\r\n}\r\n\r\ninterface MyStringLiteral\u003cValue extends string = string> extends StringLiteral {\r\n value: Value;\r\n}\r\n\r\ninterface MyTemplateLiteral\u003cValue extends string = string>\r\n extends TemplateLiteral {\r\n quasis: [Value];\r\n}\r\n\r\ntype MyStringNode\u003cS extends string = string> =\r\n | MyStringLiteral\u003cS>\r\n | MyTemplateLiteral\u003cS>;\r\n\r\ndeclare function isMyStringNode\u003cV extends string>(\r\n node: TSNode,\r\n specifics?: V,\r\n): node is MyStringNode\u003cV>;\r\n\r\nfunction foo(argument: TSNode) {\r\n if (isMyStringNode(argument)) {\r\n /*\r\n Expected: argument.type is Literal|TemplateLiteral\r\n Actual : argument.type is Literal\r\n\r\n This condition will always return 'false' since the types\r\n 'AST_NODE_TYPES.Literal' and \r\n 'AST_NODE_TYPES.TemplateLiteral' have no overlap.\r\n */\r\n if (argument.type === AST_NODE_TYPES.TemplateLiteral) {\r\n }\r\n }\r\n}\r\n```\r\n\r\nThis is a real life reproduction - it can be made smaller by removing the generics, `BaseNode`, etc, but then it becomes counterproductive as you can argue the solution to be \"just use `StringLiteral` directly\".\r\n\r\n**Expected behavior:**\r\nNo errors\r\n\r\n**Actual behavior:**\r\n\r\n`if (argument.type === AST_NODE_TYPES.TemplateLiteral) {` is marked as an error, as TS believes `argument.type` can only be `AST_NODE_TYPES.Literal` (aka it appears to discard the alternative in the union).\r\n\r\nThis behaviour is tied to the existence of the `value: string` property on `StringLiteral`: if you remove that property, everything works as expected (which is what changed between 2.12.0 & 2.13.0: previously `Literal` was just an interface w/ `type` - now it's a union of `\u003cType>Literal` interfaces; one for each of the values of `LiteralBase#value`)\r\n\r\n**Playground Link:** https://www.typescriptlang.org/play/?ts=3.8.0-dev.20191231&ssl=1&ssc=1&pln=60&pc=2#code/KYDwDg9gTgLgBAE2AYwDYEMrDsAdgVwFs4BBAZQBUB9AOQHkARAUSooE0AFJsuAbwFgAUHDgAZAJYxgUdKjgBeOAHIJUmaiUAaISIrBCYDFNXTZC5XoNHgJ9VqEBfIUNCRYcGAE8w2CmRoQSOZkMFDiuADmtmYAPnCWhujGkqaoANzOgq7Q8OFqAGboyNgAQugAzsABQQLCcOUw+Pn5AFxwBKjpji7gOXB50oXFYinqZZU4IFK4COVw41WB2LUiMgDubQ1hkRl1AG6y+MBtAEYQEKjA6LhwcQSEJ9K3cABKwBFM4M9b4RHPHV06lgIqAAPxtFYiOBgJJqXCbUK-XZQuD5DARcoI7YRZFwBy7JyCHpuXK4ApFbAhbHROSgaazEZqWQLPg6DzeY6kSi0RgsdhcMgAOhpuIOqCOWKR3SyvXcAygQ18+kSySZtKmeAZC2qyzZXh8bXI1HozFYnG4goS1hFbIAjvgKuJMfVEZEANoAXQJmXlirgAFlPFTfjSADwANUO2DpmrmP0i5njEQAfJN6XNg5Eaaz9lG2pHxcBvUTBL6KQHPFakjZRrII1G07GXdjE66U2yYzM5lXVakcyJ7Y7nW6C0cvdKhPrsIHMxEdaGeJ2GUnW9jU-I2XEZ22w2Rk5uKz2a2qF8ndkIkGhMNh8vhcMgYOIIDcndvsfPw42u83fsmABRsrgSxtH4OraHU5Q+Mg4j5OIyDlOCcDhuBACUbRAUEToVrOH5npkt73o+z6oucf6YBERB4DAIH+EsKH9v0+RwH+r5Bm2OpkVAFGEFRKH0ZCIgAPQAFRsiiIifFBUgIG05GUWSgpTv0cw0jER40mJ4kkA+DpyHAslcfJMCKRyymMqkmTiVCFAABZYcgz4IJIT43Gs4idHAshrOgnhzFgjRQDcSiFKglRKPU4TDDANnYFO5SaSiShGjypr8haNLhdcCBwAlUJJdyJp8uaQrqbWGhwDZ6B7NgQFwBA1VQBgYCCppwmCZpMHMXJPEKUp8j9Vyxq8maAqWsq1plfxmmEiIhIOEAA\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\n\r\nThis is probably a duplicate of #31156, but I've created a new issue as I believe this is a more subtle (ideally fixable) issue","author":{"url":"https://github.com/G-Rath","@type":"Person","name":"G-Rath"},"datePublished":"2020-01-02T00:23:46Z","url":"https://github.com/microsoft/TypeScript/issues/35953"}
| 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:cb49a9fd-d085-88a9-9998-fbd0a35e5477 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8A28:1201C:A55084:DDB646:6B00EBE7 |
| html-safe-nonce | e04da12ff02fc9896446ddeb51ae4af8a8a5229dea0aae9130ffe932a6bbe326 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QTI4OjEyMDFDOkE1NTA4NDpEREI2NDY6NkIwMEVCRTciLCJ2aXNpdG9yX2lkIjoiMjE2OTg4MjE0OTA1Njk5ODM3NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 538205070d8f5a9e38149ef39f078e6792ca264260631f435f7c2e9f5350ef71 |
| hovercard-subject-tag | issue:544415179 |
| 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/35953/issue_layout |
| twitter:image | https://opengraph.githubassets.com/19073f96ad316ca6644641be41ea170a38b483000bb5cd5528c1ca389e18f370/microsoft/TypeScript/issues/35953 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/19073f96ad316ca6644641be41ea170a38b483000bb5cd5528c1ca389e18f370/microsoft/TypeScript/issues/35953 |
| og:image:alt | This issue appeared in eslint-plugin-jest after updating @typescript-eslint to 2.13.0 (from 2.12.0). TypeScript Version: 3.7.x-dev.201xxxxx Search Terms: typeguard, union Code export declare enum A... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | G-Rath |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width