GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Narrow type constraints by sandersn · Pull Request #7422 · microsoft/TypeScript

Conversation

@sandersn

@sandersn Nathan Shively-Sanders (sandersn) commented Mar 7, 2016

Copy link
Copy Markdown
Member

Fixes #6445

If the narrowed type is assignable to the type parameter's constraint, then narrow to a type parameter with the constraint of the narrowed type. In the example in the bug,

isDerived(x) && isDerived(y) && x.d == y.d;

After narrowing, x and y have the type T extends Derived instead of T extends Base.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Nathan Shively-Sanders (sandersn) added 2 commits March 7, 2016 10:43
@sandersn
Get narrowed type of constrainted type parameters
4fdd759
If the narrowed type is assignable to the type parameter's *constraint*,
then narrow to a type parameter with the constraint of the narrowed type.
@sandersn
Test narrowing constrained type parameters
e5f37e4
Add test and baselines
@msftclas Microsoft Contribution License Agreements (msftclas) added the cla-not-required label Mar 7, 2016
@sandersn

Nathan Shively-Sanders (sandersn) commented Mar 8, 2016

Copy link
Copy Markdown
Member Author

As discussed with Yui (@yuit), the reason I narrow to T extends Derived instead of Derived is that I wanted to enable to returning the narrowed type. But this still doesn't work. Observe f and f2:

function f<T extends Base>(x: T) {
  return isDerived(x) && x; // f's return type appears to be `T extends Base`
}
function f2<T extends Base>(x: T): T {
  return isDerived(x) && x;
  // error: 'T' is not assignable 'T'; 'Derived' is not assignable to 'T'
}

This should work by analogy with the non-generic version:

function g(x: Base) { 
  return isDerived(x) && x; // g's return type is Derived
}
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Mar 9, 2016

Copy link
Copy Markdown
Member

It seems like if one of your return type candidates is a type parameter, you don't want to return the narrowed type there. You just want to return the type parameter itself. I can't specifically think of other types that might have this characteristic.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Mar 20, 2016
Legitimate type assertion causes compiler error #7598
Closed
@mhegazy

Mohamed Hegazy (mhegazy) commented Apr 21, 2016

Copy link
Copy Markdown
Contributor

This is already covered by #8010, closing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy Mohamed Hegazy (mhegazy) closed this Apr 21, 2016
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request May 13, 2016
Constrained types is not assignable to constrainer union types, and failed to narrow its type #8563
Closed
@mhegazy
Mohamed Hegazy (mhegazy) deleted the narrow-type-constraints branch November 2, 2017 21:02
@microsoft Microsoft (microsoft) locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@sandersn @DanielRosenwasser @mhegazy @msftclas
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.