Title: Design Meeting Notes, 8/26/2016 · Issue #10566 · microsoft/TypeScript · GitHub
Open Graph Title: Design Meeting Notes, 8/26/2016 · Issue #10566 · microsoft/TypeScript
X Title: Design Meeting Notes, 8/26/2016 · Issue #10566 · microsoft/TypeScript
Description: Expanded Inference (Open & Expando Style Types) There's lots of code out there that has no type annotations at all, no initializers, starts out as an empty object. That's mostly existing JavaScript code. Anders has been working on using ...
Open Graph Description: Expanded Inference (Open & Expando Style Types) There's lots of code out there that has no type annotations at all, no initializers, starts out as an empty object. That's mostly existing JavaScript...
X Description: Expanded Inference (Open & Expando Style Types) There's lots of code out there that has no type annotations at all, no initializers, starts out as an empty object. That's mostly existin...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/10566
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Design Meeting Notes, 8/26/2016","articleBody":"# Expanded Inference (Open & Expando Style Types)\n\nThere's lots of code out there that has no type annotations at all, no initializers, starts out as an empty object.\nThat's mostly existing JavaScript code.\nAnders has been working on using CFA to track types completely in certain situations.\n\n``` ts\nfunction foo() {\n let x;\n if (!!true) {\n x = 1;\n }\n else {\n x = \"hello\";\n }\n x; // has type 'number | string'\n}\n```\n\nThis works pretty well if we have an accurate picture of the control flow graph; however, you can't model this that well if these variables are captured in another function.\nFor instance:\n\n``` ts\nfunction foo() {\n let x;\n if (!!true) {\n x = 1;\n }\n else {\n x = \"hello\";\n }\n x; // has type 'number | string'\n\n function captor() {\n x; // implicitly has type 'any'.\n }\n}\n```\n\nThat means that 'any' has a much more limited scope of being implicitly introduced.\n\nThis makes Salsa way more powerful!\n## Expando Objects\n\nExpando is a (somewhat documented) feature and pattern in JS where you can continue tacking properties on to objects after their creation. \n\nIn certain sites, we can decide that an object is meant to be created as an \"expando\" type.\nFor instance, how useful is the type `{}` on its own? \nThe answer is \"not very useful\" (unless you're just using it for object identity), so we can build up the type as you assign properties to that object.\n\n``` ts\nlet x = {}; // after this, 'x' has type '{}'\nx.a = 1; // after this, 'x' has type '{ a: number }'\nx.b = {}; // after this, 'x' has type '{ a: number; b: {} }'\nx.b.y = \"hello\"; // after this, 'x' has type '{ a: number; b: { y: string} }'\nx; // 'x' has type '{ a: number; b: { y: string} }' here.\n```\n\nEvery occurrence of `x` above has its own type.\n\n> Side note: This is widely used and the VS JS language service lost out a lot on this in switching to Salsa.\n> Both TypeScript and the Salsa JS LS would get this for free.\n\nQ: Does this work for element access expressions?\nA: No, haven't worked that far yet.\n\nQ: Could we do this sort of thing for `this` in constructors?\nA: Potentially very bad experience - what happens if users set a value to `undefined`? It's worth experimenting with though. \n\nQ: Does this work for aliasing?\nA: No, alias tracking would get pretty messy.\n## Expanded Array Element Inference\n\nWhat about arrays?\n\nThis hasn't been implemented yet, but we have some ideas here.\nLook for locations in which the array is added to (indexing, `push`, `unshift`).\n\n``` ts\nlet x = [];\nx[0] = 4; // 'x' has type 'number[]'\nx.push(\"hello\"); // 'x' has type '(number | string)[]'\n```\n## Changes of Inference\n\n_General discussion about why an initializer is different from a deferred initialization._\n\n_Questions about inferring literal types, concerns about breaking changes._\n# `tsconfig.json` Inheritance\n- \"Is there a reason why we wouldn't?\"\n- \"No.\"\n- \"Then why don't we do it?\"\n- Open questions (duh!).\n## Merge or Overwrite?\n\nOverwrite.\n## Multiple or Single Inheritance\n\nEveryone: \"SINGLE.\"\n\nThat was easy.\n# Object Literal Rest & Spread\n\n``` ts\nlet x = { a: 0, b: \"hi\" };\nlet y = { a: true};\n\n// This is equivalent to 'Object.assign({}, x, {a: 2 }, y)'\nlet z = {\n ...x,\n a: 2,\n ...y\n};\n\n// Ideally, has type '{ a: boolean, b: string }'\nz;\n```\n\nProblem: `Object.assign` gives an intersection of its types.\n\nEasy to do when types aren't generic.\nBut what if they are?\n\n``` ts\n// What is the return type here?\nfunction merge\u003cT, U>(o: T, ext: U) {\n return {...o, ...ext};\n}\n```\n\nIdea: new type operator like `{...T, ...U}`\n\nQ: What about if `U` has an optional property that exists in `T`?\nA: Probably union the types of each property.\n## Enumerable & Own Properties\n\n``` ts\nclass C {\n a = 0;\n b = \"stuff\";\n method() {\n }\n}\n\nlet c = new C();\nlet d = { ...c };\n```\n\nProblem is that `d` doesn't get `method` because it's not enumerable or an own-property.\nWe'd need a new modifier in type members.\n# \"Optionalization\"\n\n``` ts\nfunction setState\u003cT>(o: T, ext: optional T) {\n return {...o, ...ext};\n}\n```\n\nThere are often scenarios where you want to create a type with optional portions.\nNot a lot of time to discuss this today.\n","author":{"url":"https://github.com/DanielRosenwasser","@type":"Person","name":"DanielRosenwasser"},"datePublished":"2016-08-26T19:14:30Z","url":"https://github.com/microsoft/TypeScript/issues/10566"}
| 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:20843fde-8b88-4965-05c5-f9690e1c8528 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8406:11EACE:7BFB3C:A7B405:6B00DF0F |
| html-safe-nonce | 97095799addfd025c7acdca349d88d286e1427a589b2c061151fac64898377b2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NDA2OjExRUFDRTo3QkZCM0M6QTdCNDA1OjZCMDBERjBGIiwidmlzaXRvcl9pZCI6IjQxMzgzMzA4NzU1OTcyMTc1NTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 25e5120a9aee2bc91251c493329338151eecf308671627d8744fd50324985dd8 |
| hovercard-subject-tag | issue:173523502 |
| 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/10566/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ca87b245491a438a6431512bde9a4b3e6baa093061719949694144958fd5329b/microsoft/TypeScript/issues/10566 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ca87b245491a438a6431512bde9a4b3e6baa093061719949694144958fd5329b/microsoft/TypeScript/issues/10566 |
| og:image:alt | Expanded Inference (Open & Expando Style Types) There's lots of code out there that has no type annotations at all, no initializers, starts out as an empty object. That's mostly existing JavaScript... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | DanielRosenwasser |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| 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 | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width