Title: Comparison with Facebook Flow Type System · Issue #1265 · microsoft/TypeScript · GitHub
Open Graph Title: Comparison with Facebook Flow Type System · Issue #1265 · microsoft/TypeScript
X Title: Comparison with Facebook Flow Type System · Issue #1265 · microsoft/TypeScript
Description: Disclaimer: This issue has not for purpose to prove that flow is better or worse than TypeScript, I don't want to criticize the amazing works of both team, but to list the differences in Flow and TypeScript type system and try to evaluat...
Open Graph Description: Disclaimer: This issue has not for purpose to prove that flow is better or worse than TypeScript, I don't want to criticize the amazing works of both team, but to list the differences in Flow and T...
X Description: Disclaimer: This issue has not for purpose to prove that flow is better or worse than TypeScript, I don't want to criticize the amazing works of both team, but to list the differences in Flow a...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/1265
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Comparison with Facebook Flow Type System","articleBody":"> Disclaimer: This issue has not for purpose to prove that flow is better or worse than TypeScript, I don't want to criticize the amazing works of both team, but to list the differences in Flow and TypeScript **type system** and try to evaluate which feature could improve TypeScript. \n\nAlso I won't speak about missing features in Flow since the purpose is as stated to improve TypeScript.\nFinally this topic is only about **type system** and not about supported es6/es7 features.\n## `mixed` and `any`\n\nFrom the flow doc : \n\n> - mixed: the \"supertype\" of all types. Any type can flow into a mixed.\n> - any: the \"dynamic\" type. Any type can flow into any, and vice-versa \n\nBasically that's mean that with flow `any` is the equivalent of TypeScript `any` and `mixed` is the equivalent of TypeScript `{}`.\n## The `Object` type with flow\n\nFrom flow doc : \n\n> Use mixed to annotate a location that can take anything, but do not use Object instead! It is confusing to view everything as an object, and if by any chance you do mean \"any object\", there is a better way to specify that, just as there is a way to specify \"any function\". \n\nWith TypeScript `Object` is the equivalent of `{}` and accept any type, with Flow `Object` is the equivalent of `{}` but is different than `mixed`, it will only accepts Object (and not other primitive types like `string`, `number`, `boolean`, or `function`).\n\n``` typescript\nfunction logObjectKeys(object: Object): void {\n Object.keys(object).forEach(function (key) {\n console.log(key);\n });\n}\nlogObjectKeys({ foo: 'bar' }); // valid with TypeScript and Flow\nlogObjectKeys(3); // valid with TypeScript, Error with flow\n```\n\nIn this example the parameter of `logObjectKeys` is tagged with type `Object`, for TypeScript that is the equivalent of `{}` and so it will accept any type, like a `number` in the case of the second call `logObjectKeys(3)`.\nWith Flow other primitive types are not compatible with `Object` and so the type-checker will report and error with the second call `logObjectKeys(3)` : _number is incompatible with Object_.\n## Type are non-null\n\nFrom flow doc : \n\n> In JavaScript, null implicitly converts to all the primitive types; it is also a valid inhabitant of any object type.\n> In contrast, Flow considers null to be a distinct value that is not part of any other type. \n\nsee [Flow doc section](http://flowtype.org/docs/nullable-types.html)\n\nSince the flow doc is pretty complete I won't describe this feature in details, just keep in mind that it's forcing developer to have every variables to be initialized, or marked as nullable, examples :\n\n``` typescript\nvar test: string; // error undefined is not compatible with `string`\n```\n\n``` typescript\nvar test: ?string;\nfunction getLength() {\n return test.length // error Property length cannot be initialized possibly null or undefined value\n}\n```\n\nHowever like for TypeScript type guard feature, flow understand non-null check:\n\n``` typescript\nvar test: ?string;\nfunction getLength() {\n if (test == null) {\n return 0;\n } else {\n return test.length; // no error\n }\n}\n\nfunction getLength2() {\n if (test == null) {\n test = '';\n }\n return test.length; // no error\n}\n```\n## Intersection Type\n\nsee [Flow doc section](http://flowtype.org/docs/union-intersection-types.html#intersection-example)\nsee Correspondin TypeScript issue #1256 \n\nLike TypeScript flow support union types, it also support a new way of combining types : Intersection Types.\nWith object, intersection types is like declaring a mixins : \n\n``` typescript\ntype A = { foo: string; };\ntype B = { bar : string; };\ntype AB = A & B;\n```\n\nAB has for type `{ foo: string; bar : string;}`;\n\nFor functions it is equivalent of declaring overload : \n\n``` typescript\ntype A = () => void & (t: string) => void\nvar func : A;\n```\n\nis equivalent to : \n\n``` typescript\ninterface A {\n (): void;\n (t: string): void;\n}\nvar func: A\n```\n## Generic resolution capture\n\nConsider the following TypeScript example: \n\n``` typescript\ndeclare function promisify\u003cA,B>(func: (a: A) => B): (a: A) => Promise\u003cB>;\ndeclare function identity\u003cA>(a: A): A;\n\nvar promisifiedIdentity = promisify(identity);\n```\n\nWith TypeScript `promisifiedIdentity` will have for type:\n\n``` typescript\n(a: {}) => Promise\u003c{}>`.\n```\n\nWith flow `promisifiedIdentity` will have for type: \n\n``` typescript\n\u003cA>(a: A) => Promise\u003cA>\n```\n## Type inference\n\nFlow in general try to infer more type than TypeScript.\n### Inference of parameters\n\nLet's give a look at this example : \n\n``` typescript\nfunction logLength(obj) {\n console.log(obj.length);\n}\nlogLength({length: 'hello'});\nlogLength([]);\nlogLength(\"hey\");\nlogLength(3);\n```\n\nWith TypeScript, no errors are reported, with flow the last call of `logLength` will result in an error because `number` does not have a `length` property.\n### Inferred type changes with usage\n\nWith flow unless you expressly type your variable, the type of this variable will change with the usage of this variable : \n\n```\nvar x = \"5\"; // x is inferred as string\nconsole.log(x.length); // ok x is a string and so has a length property\nx = 5; // Inferred type is updated to `number`\nx *= 5; // valid since x is now a number\n```\n\nIn this example x has initially `string` type, but when assigned to a number the type has been changed to `number`.\nWith typescript the assignation `x = 5` would result in an error since `x` was previously assigned to `string` and its type cannot change.\n### Inference of Union types\n\nAnother difference is that Flow propagates type inference backwards to broaden the inferred type into a type union. This example is from facebook/flow#67 (comment)\n\n``` typescript\nclass A { x: string; }\nclass B extends A { y: number; }\nclass C extends A { z: number; }\n\nfunction foo() {\n var a = new B();\n if (true) a = new C(); // TypeScript reports an error, because a's type is already too narrow\n a.x; // Flow reports no error, because a's type is correctly inferred to be B | C\n}\n```\n\n(\"correctly\" is from the original post.)\nSince flow detected that the `a` variable could have `B` type or `C` type depending of a conditional statement, it is now inferred to `B | C`, and so the statement `a.x` does not result in an error since both types has an `x` property, if we would have tried to access the `z` property and error would have been raised.\n\nThis means the following will compile too.\n\n``` typescript\nvar x = \"5\"; // x is inferred as string\nif ( true) { x = 5; } // Inferred type is updated to string | number\nx.toString(); // Compiles\nx += 5; // Compiles. Addition is defined for both string and number after all, although the result is very different\n```\n## Edit\n- Updated the `mixed` and `any` section, since `mixed` is the equivalent of `{}` there is no need for example.\n- Added section for the `Object` type.\n- Added section on type inference\n\n_Feel free to notify if I forgot something I'll try to update the issue._\n","author":{"url":"https://github.com/fdecampredon","@type":"Person","name":"fdecampredon"},"datePublished":"2014-11-25T15:39:28Z","url":"https://github.com/microsoft/TypeScript/issues/1265"}
| 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:c903c721-c5da-0aa8-5b2b-4c8f5c410dcf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AC2C:785A9:1E63082:2681DCA:6B016290 |
| html-safe-nonce | b9a72d269d3215d415db12aa385b41db4bcc043b00a87fae402ef7e8719e0825 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQzJDOjc4NUE5OjFFNjMwODI6MjY4MURDQTo2QjAxNjI5MCIsInZpc2l0b3JfaWQiOiI5MDA5OTUxNTc4OTUzNzA0MDgwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f5d9746859e641e197dc801b1471e999976141e87b7cd8f385eb8c5890222847 |
| hovercard-subject-tag | issue:50042690 |
| 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/1265/issue_layout |
| twitter:image | https://opengraph.githubassets.com/17f4c406b00df6104b296ddcb5a6cd8a573e28c98f52fa9aca6a3dfa1856303f/microsoft/TypeScript/issues/1265 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/17f4c406b00df6104b296ddcb5a6cd8a573e28c98f52fa9aca6a3dfa1856303f/microsoft/TypeScript/issues/1265 |
| og:image:alt | Disclaimer: This issue has not for purpose to prove that flow is better or worse than TypeScript, I don't want to criticize the amazing works of both team, but to list the differences in Flow and T... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | fdecampredon |
| 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