René's URL Explorer Experiment


Title: Type predicate expressions and 'this' type assertions · Issue #12942 · microsoft/TypeScript · GitHub

Open Graph Title: Type predicate expressions and 'this' type assertions · Issue #12942 · microsoft/TypeScript

X Title: Type predicate expressions and 'this' type assertions · Issue #12942 · microsoft/TypeScript

Description: The type system currently allows to specify basic type constraints (or 'assertions') on polymorphic types, for example: function example>(a: T) {}; However, currently there is no elegant syntax for types to declare c...

Open Graph Description: The type system currently allows to specify basic type constraints (or 'assertions') on polymorphic types, for example: function example>(a: T) {}; However, currently there is ...

X Description: The type system currently allows to specify basic type constraints (or 'assertions') on polymorphic types, for example: function example<T extends Array<any>>(a: T) {}; However,...

Opengraph URL: https://github.com/microsoft/TypeScript/issues/12942

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type predicate expressions and 'this' type assertions","articleBody":"The type system currently allows to specify basic type constraints (or 'assertions') on polymorphic types, for example:\r\n\r\n```ts\r\nfunction example\u003cT extends Array\u003cany>>(a: T) {};\r\n```\r\n\r\nHowever, currently there is no elegant syntax for types to declare constraints on _themselves_. Here's a real world example where I needed something like this:\r\n\r\nI have a class containing several methods:\r\n\r\n```ts\r\nclass MyClass {\r\n\tread(filename: string, byteCount: number): string {\r\n\t\t// ...\r\n\t}\r\n\r\n\twrite(filename: string, text: string): void {\r\n\t\t// ...\r\n\t}\r\n\r\n\tdelete(filename: string): void {\r\n\t\t// ...\r\n\t}\r\n}\r\n```\r\n\r\nand I want to write a known (i.e. monomorphic) type that is constrained to exactly match the _keys_ of `MyClass` (the property types don't matter for this example, so I annotated them with ` { ... }`, for simplicity, also note that `MyType` is intended to be user-customizable and cannot be deterministically generated using a mapped type):\r\n\r\n```ts\r\ntype MyType = {\r\n\tread: { ... };\r\n\twrite: { ... };\r\n\tdelete: { ... };\r\n}\r\n```\r\n\r\nThe best hack/workaround I found so far was to declare additional dummy types, that would fail if `MyType` doesn't satisfy the expected constraint:\r\n\r\n```ts\r\ntype Extends\u003cT1 extends T2, T2> = boolean; // The 'boolean' doesn't really have any meaning here.\r\n\t\t\t\t\t// It can technically be any type.\r\n\t\t\t\t\t// I only used it because I needed to fill out some sort of\r\n\t\t\t\t\t// type association to satisfy the compiler.\r\n\r\n// These types are never actually used. They are only here to force the program not to type-check \r\n// if their supplied arguments don't satisfy the constraints for the Extends\u003c..> type parameters:\r\ntype DummyAssertionType1 = Extends\u003ckeyof MyType, keyof MyClass>;\r\ntype DummyAssertionType2 = Extends\u003ckeyof MyClass, keyof MyType>;\r\n```\r\n\r\nThe hack does seems to work so far, and has proven useful in practice, so by itself that's a significant improvement over having nothing at all. However, it is not very elegant or readable and violates type 'encapsulation'. It would be cool if I could instead write something like:\r\n\r\n```ts\r\ntype SameKeys\u003cT1, T2> = (keyof T1 extends keyof T2) && (keyof T2 extends keyof T1);\r\n\r\ntype MyType where SameKeys\u003cthis, MyClass> = {\r\n\tread: { ... };\r\n\twrite: { ... };\r\n\tdelete: { ... };\r\n}\r\n```\r\n\r\nI'll try to break down what is happening here:\r\n\r\n* The `SameKeys` type contains a type expression that evaluates to either the `true` or `false` boolean literal types. Given `P1`, `P2` are valid type predicate expressions (e.g. `T extends U`) then `P1 && P2`, `P1 || P2` `!P1` are also type predicate expressions etc.\r\n* The `where` keyword simply implies that the following type or type expression must evaluate to a boolean literal type and in particular to `true` for the constrained type to be valid.\r\n\r\n_[Edit: removed some confusing examples, see the next comment]_\r\n\r\nThe concept can also be used with interfaces, or even classes:\r\n\r\n```ts\r\ninterface MyInterface\u003cT, U> where SomeComplexPredicate\u003cthis, T, U> { \r\n  // ...\r\n}\r\n\r\nclass MyClass\u003cT, U> where SomeComplexPredicate\u003cthis, T, U> { \r\n  //...\r\n}\r\n```\r\n\r\n_Edit: changed the suggested `satisfies` keyword to `where` as I believe it gives slightly better readability._","author":{"url":"https://github.com/rotemdan","@type":"Person","name":"rotemdan"},"datePublished":"2016-12-15T11:26:49Z","url":"https://github.com/microsoft/TypeScript/issues/12942"}

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:ff44af55-4393-5a37-44b1-fbdf2120b7db
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD834:38C5CF:96D18A:C424E2:6B00DE0D
html-safe-nonce4184066d88ef0cf305924b795f5f14bb2a0e321f47b412a878e9899ca5cc3837
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEODM0OjM4QzVDRjo5NkQxOEE6QzQyNEUyOjZCMDBERTBEIiwidmlzaXRvcl9pZCI6IjQwNTk1MTQ4NjcwNTExOTE4MjEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmaceb3c7f9ad83ac75b5257c4cbdabcf2654146a3be04a12e96e9b4107e2c505282
hovercard-subject-tagissue:195781654
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/12942/issue_layout
twitter:imagehttps://opengraph.githubassets.com/de954bfe3e961597f2c1686dd237a95fd1e2ad0fa389c6a3aa639a9211756d83/microsoft/TypeScript/issues/12942
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/de954bfe3e961597f2c1686dd237a95fd1e2ad0fa389c6a3aa639a9211756d83/microsoft/TypeScript/issues/12942
og:image:altThe type system currently allows to specify basic type constraints (or 'assertions') on polymorphic types, for example: function example>(a: T) {}; However, currently there is ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamerotemdan
hostnamegithub.com
expected-hostnamegithub.com
None043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf
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
release5425dc24e8bd8d6fca9641a12afc984825a43875
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/12942#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F12942
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%2F12942
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/12942
Reloadhttps://github.com/microsoft/TypeScript/issues/12942
Reloadhttps://github.com/microsoft/TypeScript/issues/12942
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests308 (308)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/12942
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/12942
Type predicate expressions and 'this' type assertionshttps://github.com/microsoft/TypeScript/issues/12942#top
https://github.com/rotemdan
Rotem Dan (rotemdan)https://github.com/rotemdan
on Dec 15, 2016https://github.com/microsoft/TypeScript/issues/12942#issue-195781654
Rotem Dan (rotemdan)https://github.com/rotemdan
on Dec 15, 2016https://github.com/microsoft/TypeScript/issues/12942#event-895115142
Rotem Dan (rotemdan)https://github.com/rotemdan
on Dec 15, 2016https://github.com/microsoft/TypeScript/issues/12942#event-895185220
https://github.com/rotemdan
Rotem Dan (rotemdan)https://github.com/rotemdan
on Dec 16, 2016https://github.com/microsoft/TypeScript/issues/12942#issuecomment-267579765
Rotem Dan (rotemdan)https://github.com/rotemdan
on Dec 16, 2016https://github.com/microsoft/TypeScript/issues/12942#event-896635290
https://github.com/rotemdan
Rotem Dan (rotemdan)https://github.com/rotemdan
on Dec 16, 2016https://github.com/microsoft/TypeScript/issues/12942#issuecomment-267591885
https://github.com/rotemdan
Rotem Dan (rotemdan)https://github.com/rotemdan
on Jan 13, 2017https://github.com/microsoft/TypeScript/issues/12942#issuecomment-272407903
Rotem Dan (rotemdan)https://github.com/rotemdan
completedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Jan 13, 2017https://github.com/microsoft/TypeScript/issues/12942#event-922032733
microsofthttps://github.com/microsoft
on Jun 19, 2018https://github.com/microsoft/TypeScript/issues/12942#event-1689925089
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/12942
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/12942
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.