René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:5bb94e63-3f9a-02d2-8631-639ff3d04a72
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idBCF4:96DDF:19DB13:21815D:6B00C9CE
html-safe-nonce1031fc41ebdf4f08201f7c7089b2ee92c9f308f560c018d0e6b80c33de463d56
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQ0Y0Ojk2RERGOjE5REIxMzoyMTgxNUQ6NkIwMEM5Q0UiLCJ2aXNpdG9yX2lkIjoiODYyNzcxNDU1OTYyNjk2MTQyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac3a79c2a5a30450c43fae5413c3330567031109d7a607cd655fa2e487c4b2a8df
hovercard-subject-tagissue:129351797
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/6686/issue_layout
twitter:imagehttps://opengraph.githubassets.com/2e81a84e22cde4d987b234f6397ccd35b60093f16da831b38dab3415e210aaed/microsoft/TypeScript/issues/6686
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/2e81a84e22cde4d987b234f6397ccd35b60093f16da831b38dab3415e210aaed/microsoft/TypeScript/issues/6686
og:image:altThis 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameyortus
hostnamegithub.com
expected-hostnamegithub.com
None98dc00e171c204462a6f367482a0f8c3c6b5dbca99cd7390e82d2df49ce096e4
turbo-cache-controlno-preview
go-importgithub.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git
octolytics-dimension-user_id6154722
octolytics-dimension-user_loginmicrosoft
octolytics-dimension-repository_id20929025
octolytics-dimension-repository_nwomicrosoft/TypeScript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id20929025
octolytics-dimension-repository_network_root_nwomicrosoft/TypeScript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseef013399cf0a3c324bea644a685d7c1e0f06b5b7
ui-targetcanary-1
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/6686#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F6686
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F6686
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=microsoft%2FTypeScript
Reloadhttps://github.com/microsoft/TypeScript/issues/6686
Reloadhttps://github.com/microsoft/TypeScript/issues/6686
Reloadhttps://github.com/microsoft/TypeScript/issues/6686
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests307 (307)https://github.com/microsoft/TypeScript/pulls
Actionshttps://github.com/microsoft/TypeScript/actions
Projectshttps://github.com/microsoft/TypeScript/projects
Wikihttps://github.com/microsoft/TypeScript/wiki
Security and qualityhttps://github.com/microsoft/TypeScript/security
Insightshttps://github.com/microsoft/TypeScript/insights
https://github.com/microsoft/TypeScript
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/6686
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/6686
Suggestion for improving generator and async function type checkinghttps://github.com/microsoft/TypeScript/issues/6686#top
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com/yortus
Troy Gerwien (yortus)https://github.com/yortus
on Jan 28, 2016https://github.com/microsoft/TypeScript/issues/6686#issue-129351797
a6af98ehttps://github.com/microsoft/TypeScript/commit/a6af98e10025492e0afda315ea37cfebe0b2bbfb
herehttps://github.com/Microsoft/TypeScript/issues/5911#issuecomment-175866624
generator objecthttp://www.ecma-international.org/ecma-262/6.0/#sec-generator-objects
herehttps://github.com/Microsoft/TypeScript/issues/5911#issuecomment-175866624
herehttps://github.com/Microsoft/TypeScript/pull/6631#discussion_r51040975
herehttps://github.com/yortus/TypeScript/compare/master...yortus:gen-async-func-return-types
https://github.com/yortus
Troy Gerwien (yortus)https://github.com/yortus
on Jan 28, 2016https://github.com/microsoft/TypeScript/issues/6686#issuecomment-176188318
https://github.com/mhegazy
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
on Jan 28, 2016https://github.com/microsoft/TypeScript/issues/6686#issuecomment-176432639
#6068https://github.com/microsoft/TypeScript/issues/6068
https://github.com/yortus
Troy Gerwien (yortus)https://github.com/yortus
on Jan 28, 2016https://github.com/microsoft/TypeScript/issues/6686#issuecomment-176438965
Mohamed Hegazy (@mhegazy)https://github.com/mhegazy
#6068https://github.com/microsoft/TypeScript/issues/6068
https://github.com/yortus
Troy Gerwien (yortus)https://github.com/yortus
on Jan 28, 2016https://github.com/microsoft/TypeScript/issues/6686#issuecomment-176446713
Mohamed Hegazy (@mhegazy)https://github.com/mhegazy
https://github.com/mhegazy
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
on Jan 28, 2016https://github.com/microsoft/TypeScript/issues/6686#issuecomment-176448561
https://github.com/yortus
Troy Gerwien (yortus)https://github.com/yortus
on Jan 28, 2016https://github.com/microsoft/TypeScript/issues/6686#issuecomment-176461417
#5911https://github.com/microsoft/TypeScript/issues/5911
#6631https://github.com/microsoft/TypeScript/pull/6631
https://github.com/jods4
jods (jods4)https://github.com/jods4
on Jan 29, 2016https://github.com/microsoft/TypeScript/issues/6686#issuecomment-176758649
Troy Gerwien (@yortus)https://github.com/yortus
#6631https://github.com/microsoft/TypeScript/pull/6631
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%22
on Feb 20, 2016https://github.com/microsoft/TypeScript/issues/6686#event-557393659
https://github.com/mhegazy
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
on Feb 20, 2016https://github.com/microsoft/TypeScript/issues/6686#issuecomment-186509927
jods (@jods4)https://github.com/jods4
https://github.com/yortus
Troy Gerwien (yortus)https://github.com/yortus
on Feb 20, 2016https://github.com/microsoft/TypeScript/issues/6686#issuecomment-186514319
Mohamed Hegazy (@mhegazy)https://github.com/mhegazy
jods (@jods4)https://github.com/jods4
jods (@jods4)https://github.com/jods4
jods (@jods4)https://github.com/jods4
jods (@jods4)https://github.com/jods4
https://github.com/yortus
Troy Gerwien (yortus)https://github.com/yortus
on Feb 20, 2016https://github.com/microsoft/TypeScript/issues/6686#issuecomment-186517358
jods (@jods4)https://github.com/jods4
Liskov substitution principlehttps://en.wikipedia.org/wiki/Liskov_substitution_principle
https://github.com/nwalters512
Nathan Sarang-Walters (nwalters512)https://github.com/nwalters512
on Jul 18, 2018https://github.com/microsoft/TypeScript/issues/6686#issuecomment-406072596
https://github.com/yortus
Troy Gerwien (yortus)https://github.com/yortus
on Jul 19, 2018https://github.com/microsoft/TypeScript/issues/6686#issuecomment-406121352
Nathan Sarang-Walters (@nwalters512)https://github.com/nwalters512
https://github.com/nwalters512
Nathan Sarang-Walters (nwalters512)https://github.com/nwalters512
on Jul 19, 2018https://github.com/microsoft/TypeScript/issues/6686#issuecomment-406126431
https://github.com/yortus
Troy Gerwien (yortus)https://github.com/yortus
on Jul 19, 2018https://github.com/microsoft/TypeScript/issues/6686#issuecomment-406129587
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/6686
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/6686
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.