Title: Suggestion for improving generator and async function type checking · Issue #6686 · microsoft/TypeScript · GitHub
Open Graph Title: Suggestion for improving generator and async function type checking · Issue #6686 · microsoft/TypeScript
X Title: Suggestion for improving generator and async function type checking · Issue #6686 · microsoft/TypeScript
Description: This is a suggestion to improve both the consistency and the type-safety of return type checking for generator functions (GFs) and async functions (AFs). NB: this issue refers to tsc behaviour for target >=ES6 with typescript@next as of ...
Open Graph Description: This is a suggestion to improve both the consistency and the type-safety of return type checking for generator functions (GFs) and async functions (AFs). NB: this issue refers to tsc behaviour for ...
X Description: This is a suggestion to improve both the consistency and the type-safety of return type checking for generator functions (GFs) and async functions (AFs). NB: this issue refers to tsc behaviour for ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/6686
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion for improving generator and async function type checking","articleBody":"This is a suggestion to improve both the consistency and the type-safety of return type checking for\ngenerator functions (GFs) and async functions (AFs).\n\nNB: this issue refers to `tsc` behaviour for target >=ES6 with `typescript@next` as of 2016-01-28 (ie since commit https://github.com/Microsoft/TypeScript/commit/a6af98e10025492e0afda315ea37cfebe0b2bbfb)\n## Current Behaviour\n\nConsider the following examples of current behaviour for checking return type annotations:\n\n``` typescript\n// Current behaviour - generator functions\nclass MyIterator implements Iterator\u003cany> {next}\nfunction* gf1(): any {} // OK\nfunction* gf2(): MyIterator {} // OK (but really is an error)\n\n// Current behaviour - async functions\nclass MyPromise extends Promise\u003cany> {}\nasync function af1(): any {} // ERROR (but is really not an error)\nasync function af2(): MyPromise {} // ERROR\n```\n## Problems with Current Behaviour\n\nFirstly, type checking is not consistent across the two kinds of functions. In the examples the GF checking is too loose and the AF checking is too strict. The inconsistency is due to the different approach to checking the return type. The two approaches may be summarised like this:\n- _Generator functions:_ accept any return type annotation that is assignable from `IterableIterator\u003cT>`.\n- _Async functions:_ reject all return type annotations other than references to the global `Promise\u003cT>`. This is a recent change, the rationale\n for it can be followed from [here](https://github.com/Microsoft/TypeScript/issues/5911#issuecomment-175866624).\n\nSecondly, the type checker only gets 2 out of 4 of the above checks right (`gf1` and `af2`). Explanation:\n- GOOD: `gf1`'s return type annotation is not super helpful but is 100% consistent with the type system. No sense erroring here, so the implementation is good.\n- BAD: `gf2`'s return type annotation passes type checking because it passes the assignability check.\n However `gf2` definitely does not return an instance of `MyIterator`. All generator functions\n return a [generator object](http://www.ecma-international.org/ecma-262/6.0/#sec-generator-objects), so at runtime `gf2() instanceof MyIterator` is `false`. A compile error would have been helpful.\n- BAD: `af1`'s return type annotation is just like `gf1`: not super helpful but 100% consistent with the type system. The compiler errors here even though nothing is wrong (reason for the error is [here](https://github.com/Microsoft/TypeScript/issues/5911#issuecomment-175866624)).\n- GOOD: `af2`'s return type annotation fails type checking because it's not `Promise\u003cT>`. The return type definitely won't be an instance of any class other than `Promise`, so the implementation is good.\n## Suggested Improvement\n\n_Since GFs and AFs always return instances of known instrinsic types, we can rule out any type annotation that asserts they will return an instance of some other class._\n\nBoth generator and async functions could therefore be checked with the same two steps:\n1. _Is Assignable:_ Ensure the return type of the GF/AF is assignable to the annotated return type. This is the basic check for all kinds of function return types. If not assignable, type checking fails. Otherwise, continue to step 2.\n2. _Not a Class Type:_ Ensure the return type annotation is not a class type (except `Promise\u003cT>` which is allowed for AFs). For example if the return type annotation is `Foo`, ensure it does not refer to `class Foo {...}` or another class-like value.\n\nThese rules have the following effects:\n- GF and AF type checking are mutually consistent.\n- This fixes `gf2` by ruling out class types like `MyIterator` in addition to checking assignability. GF type checking is made safer in general by catching a class of errors that currently slip through.\n- This fixes `af1`, because it's no longer necessary to rule out _all_ annotations other than `Promise\u003cT>`, but just those that are assignment-compatible class types like `MyPromise`. This approach will catch the breaking change from 1.7 as a compile error (as desired for reason [here](https://github.com/Microsoft/TypeScript/pull/6631#discussion_r51040975)), but allow harmless (and correct) things like `any` and `PromiseLike\u003cT>`.\n## Working Implementation\n\nThis is a small code change. I implemented this in a branch as best I could (but I may have made errors since I'm still getting my head around the codebase). The diff can be seen [here](https://github.com/yortus/TypeScript/compare/master...yortus:gen-async-func-return-types).\n\nWith this version of `tsc` the above code works as follows:\n\n``` typescript\n// Suggested behaviour - generator functions\nclass MyIterator implements Iterator\u003cany> {next}\nfunction* gf1(): any {} // OK\nfunction* gf2(): MyIterator {} // ERROR: A generator cannot have a return type annotation of a class type.\n\n// Suggested behaviour - async functions\nclass MyPromise extends Promise\u003cany> {}\nasync function af1(): any {} // OK\nasync function af2(): MyPromise {} // ERROR: An async function cannot have a return type annotation of a class type other than Promise\u003cT>.\n```\n","author":{"url":"https://github.com/yortus","@type":"Person","name":"yortus"},"datePublished":"2016-01-28T06:18:13Z","url":"https://github.com/microsoft/TypeScript/issues/6686"}
| 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:5bb94e63-3f9a-02d2-8631-639ff3d04a72 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BCF4:96DDF:19DB13:21815D:6B00C9CE |
| html-safe-nonce | 1031fc41ebdf4f08201f7c7089b2ee92c9f308f560c018d0e6b80c33de463d56 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQ0Y0Ojk2RERGOjE5REIxMzoyMTgxNUQ6NkIwMEM5Q0UiLCJ2aXNpdG9yX2lkIjoiODYyNzcxNDU1OTYyNjk2MTQyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 3a79c2a5a30450c43fae5413c3330567031109d7a607cd655fa2e487c4b2a8df |
| hovercard-subject-tag | issue:129351797 |
| 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/6686/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2e81a84e22cde4d987b234f6397ccd35b60093f16da831b38dab3415e210aaed/microsoft/TypeScript/issues/6686 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2e81a84e22cde4d987b234f6397ccd35b60093f16da831b38dab3415e210aaed/microsoft/TypeScript/issues/6686 |
| og:image:alt | This is a suggestion to improve both the consistency and the type-safety of return type checking for generator functions (GFs) and async functions (AFs). NB: this issue refers to tsc behaviour for ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | yortus |
| hostname | github.com |
| expected-hostname | github.com |
| None | 98dc00e171c204462a6f367482a0f8c3c6b5dbca99cd7390e82d2df49ce096e4 |
| 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 | ef013399cf0a3c324bea644a685d7c1e0f06b5b7 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width