Title: Breaking changes associated with Union Types · Issue #868 · microsoft/TypeScript · GitHub
Open Graph Title: Breaking changes associated with Union Types · Issue #868 · microsoft/TypeScript
X Title: Breaking changes associated with Union Types · Issue #868 · microsoft/TypeScript
Description: This issue tracks the set of breaking changes that result from the change to union types #824 to implement the suggestion #805. The issues here will be formalized further once an official spec update is done, until then it will mostly se...
Open Graph Description: This issue tracks the set of breaking changes that result from the change to union types #824 to implement the suggestion #805. The issues here will be formalized further once an official spec upda...
X Description: This issue tracks the set of breaking changes that result from the change to union types #824 to implement the suggestion #805. The issues here will be formalized further once an official spec upda...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/868
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Breaking changes associated with Union Types","articleBody":"This issue tracks the set of breaking changes that result from the change to union types #824 to implement the suggestion #805. The issues here will be formalized further once an official spec update is done, until then it will mostly serve as an easy reference for the type of coding patterns that have broken for various reasons.\n#### Multiple Best Common Type Candidates\n\nGiven multiple viable candidates from a Best Common Type computation we now choose an item (depending on the compiler's implementation) rather than the first item.\n\n``` ts\nvar a: { x: number; y?: number };\nvar b: { x: number; z?: number };\n\n// was { x: number; z?: number; }[]\n// now { x: number; y?: number; }[]\nvar bs = [b, a]; \n```\n\nThis can happen in a variety of circumstances. A shared set of required properties and a disjoint set of other properties (optional or otherwise), empty types, compatible signature types (including generic and non-generic signatures when type parameters are stamped out with `any`).\n\n**Recommendation**\nProvide a type annotation if you need a specific type to be chosen\n\n``` ts\nvar bs: { x: number; y?: number; z?: number }[] = [b, a];\n```\n#### Generic Type Inference\n\nUsing different types for multiple arguments of type T is now an error, even with constraints involved:\n\n``` ts\ndeclare function foo\u003cT>(x: T, y:T): T;\nvar r = foo(1, \"\"); // r used to be {}, now this is an error\n```\n\nWith constraints:\n\n``` ts\ninterface Animal { x }\ninterface Giraffe extends Animal { y }\ninterface Elephant extends Animal { z }\nfunction f\u003cT extends Animal>(x: T, y: T): T { return undefined; }\nvar g: Giraffe;\nvar e: Elephant;\nf(g, e);\n```\n\nSee https://github.com/Microsoft/TypeScript/pull/824#discussion_r18665727 for explanation.\n\n**Recommendations**\nSpecify an explicit type parameter if the mismatch was intentional:\n\n``` ts\nvar r = foo\u003c{}>(1, \"\"); // Emulates 1.0 behavior\nvar r = foo\u003cstring|number>(1, \"\"); // Most useful\nvar r = foo\u003cany>(1, \"\"); // Easiest\nf\u003cAnimal>(g, e);\n```\n\n_or_ rewrite the function definition to specify that mismatches are OK:\n\n``` ts\ndeclare function foo\u003cT,U>(x: T, y:U): T|U;\nfunction f\u003cT extends Animal, U extends Animal>(x: T, y: U): T|U { return undefined; }\n```\n#### Generic Rest Parameters\n\nYou cannot use hetergeneous argument types anymore:\n\n``` ts\nfunction makeArray\u003cT>(...items: T[]): T[] { return items; }\nvar r = makeArray(1, \"\"); // used to return {}[], now an error\n```\n\nLikewise for `new Array(...)`\n\n**Recommendations**\nDeclare a back-compat signature if the 1.0 behavior was desired:\n\n``` ts\nfunction makeArray\u003cT>(...items: T[]): T[];\nfunction makeArray(...items: {}[]): {}[];\nfunction makeArray\u003cT>(...items: T[]): T[] { return items; }\n```\n#### Overload Resolution with Type Argument Inference\n\n``` ts\nvar f10: \u003cT>(x: T, b: () => (a: T) => void, y: T) => T;\nvar r9 = f10('', () => (a => a.foo), 1); // r9 was any, now this is an error\n```\n\n**Recommendations**\nManually specify a type parameter\n\n``` ts\nvar r9 = f10\u003cany>('', () => (a => a.foo), 1);\n```\n#### Promises\n\nThis used to not have an error:\n\n``` ts\ninterface Promise\u003cT> {\n then\u003cU>(success?: (value: T) => Promise\u003cU>, error?: (error: any) => Promise\u003cU>, progress?: (progress: any) => void): Promise\u003cU>;\n then\u003cU>(success?: (value: T) => Promise\u003cU>, error?: (error: any) => U, progress?: (progress: any) => void): Promise\u003cU>;\n then\u003cU>(success?: (value: T) => U, error?: (error: any) => Promise\u003cU>, progress?: (progress: any) => void): Promise\u003cU>;\n then\u003cU>(success?: (value: T) => U, error?: (error: any) => U, progress?: (progress: any) => void): Promise\u003cU>;\n done\u003cU>(success?: (value: T) => any, error?: (error: any) => any, progress?: (progress: any) => void): void;\n}\n\ninterface IPromise\u003cT> {\n then\u003cU>(success?: (value: T) => IPromise\u003cU>, error?: (error: any) => IPromise\u003cU>, progress?: (progress: any) => void): IPromise\u003cU>;\n then\u003cU>(success?: (value: T) => IPromise\u003cU>, error?: (error: any) => U, progress?: (progress: any) => void): IPromise\u003cU>;\n then\u003cU>(success?: (value: T) => U, error?: (error: any) => IPromise\u003cU>, progress?: (progress: any) => void): IPromise\u003cU>;\n then\u003cU>(success?: (value: T) => U, error?: (error: any) => U, progress?: (progress: any) => void): IPromise\u003cU>;\n done? \u003cU>(success?: (value: T) => any, error?: (error: any) => any, progress?: (progress: any) => void): void;\n}\n\ndeclare function f1(x: number): IPromise\u003cnumber>;\ndeclare function f2(x: number): Promise\u003cnumber>;\nvar p: Promise\u003cnumber>;\nvar r = p.then(f2, f1, f1).then(f1, f1, f1);\n// ^^\n// error TS2345: Argument of type '(x: number) => IPromise\u003cnumber>' is not assignable to parameter of type '(value: IPromise\u003cnumber>) => IPromise\u003cnumber>'.\n```\n","author":{"url":"https://github.com/danquirk","@type":"Person","name":"danquirk"},"datePublished":"2014-10-10T01:01:33Z","url":"https://github.com/microsoft/TypeScript/issues/868"}
| 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:bef8fb2b-eb60-f450-1e5b-55a316394601 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8A8A:2BB4ED:3B02DF:515F4A:6B008C49 |
| html-safe-nonce | 921b73c23453296696c2e475fa6b67485ded47accdd86a700242afc2ce5d3860 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QThBOjJCQjRFRDozQjAyREY6NTE1RjRBOjZCMDA4QzQ5IiwidmlzaXRvcl9pZCI6IjkxODE5NDM3Mzc1OTU0OTc1NDYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ad58f0156f289bc0ad68803769d930b05977bde78a9ef0a8546ce66d04b58712 |
| hovercard-subject-tag | issue:45435707 |
| 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/868/issue_layout |
| twitter:image | https://opengraph.githubassets.com/96e143d58f0780f428c1cf06514378a6cbaecf2735ec0e1564d827954ba8a1b5/microsoft/TypeScript/issues/868 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/96e143d58f0780f428c1cf06514378a6cbaecf2735ec0e1564d827954ba8a1b5/microsoft/TypeScript/issues/868 |
| og:image:alt | This issue tracks the set of breaking changes that result from the change to union types #824 to implement the suggestion #805. The issues here will be formalized further once an official spec upda... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | danquirk |
| hostname | github.com |
| expected-hostname | github.com |
| None | c710a5cb5c47be60abfe6e8410436d409906979dbed614ef09f29c60f5535b08 |
| 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 | be616a2a6ec1c609d6e002896dff5352d5c5f28b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width