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 — WIP: Member-type based type guards by weswigham · Pull Request #6062 · microsoft/TypeScript

Conversation

@weswigham

@weswigham Wesley Wigham (weswigham) commented Dec 11, 2015 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

This PR adds type guards of the form:

if (foo.bar.kind === discriminatorType) {
  // foo potentially narrowed
}

Related issues: #186, probably more.

Known issues/TODOs:

  • Comparing against a string doesn't yield a string type specialization, it just gets a string type. I need to get the contextual type of the literal (hence the not-correct changes in stringLiteralTypesInUnionTypes02). It feels awkward that getTypeOfExpression doesn't just return the string type and that I need to special case it, though I would like some advice on how to best do this. Daniel Rosenwasser (@DanielRosenwasser), you should know more about this, what's the best thing to do here?
  • Other type guards don't narrow member types down yet (only property lookup === expr does). I need to rewire the other type guards to allow for nested narrowing.
  • The false branch of a property lookup === expr guard doesn't narrow yet. I'm not 100% sure on what's "safe" to remove in the false branch (probably just nested incompatible union members), though I'm pretty certain it should only do anything when the RHS type is a value literal type (ie, string literal type).
  • I'm pretty sure statements like if (foo["bar"]["kind"] === discriminatorType) don't narrow right now, as I don't handle ElementAccessExpressions yet.

Insights/inputs are welcome - Mohamed Hegazy (@mhegazy) mentioned he wanted me to look into these the other day, and I noticed they've been added to the roadmap for 2.0.

Sorry, something went wrong.

Uh oh!

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

svieira, mquandalle, and michaelmesser reacted with thumbs up emoji michaelmesser reacted with hooray emoji michaelmesser reacted with heart emoji
All reactions
  • 👍 3 reactions
  • 🎉 1 reaction
  • ❤️ 1 reaction
@msftclas Microsoft Contribution License Agreements (msftclas) added the cla-already-signed label Dec 11, 2015
DanielRosenwasser
DanielRosenwasser reviewed Dec 11, 2015
View reviewed changes
Comment thread src/compiler/checker.ts Outdated

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Dec 11, 2015

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document what a selector is.

Sorry, something went wrong.

Uh oh!

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

All reactions
@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Dec 12, 2015

Copy link
Copy Markdown
Member

I think we could discuss some issues with string literals with widening semantics. If you'd like, I'll look into what a widening approach would look like based on our earlier conversation.

All reactions

Sorry, something went wrong.

Uh oh!

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

@weswigham

Wesley Wigham (weswigham) commented Dec 12, 2015

Copy link
Copy Markdown
Member Author

Daniel Rosenwasser (@DanielRosenwasser) I've looked at it - string literal types just have to be contextually typed. Mostly because (unlike predicate types) they can appear inside arrays and object literals... and in all those cases we want them to widen away... except when we don't because there's a type annotation on the thing the literal is getting assigned to. And thus the contextual type is born - informing the type of the RHS by the type defined on the LHS side.

So, my real issue: I want to run checkExpression but where the RHS can be contextually typed to a string literal type. I guess that means adding a case for EqualsEqualsEquals to getContextualTypeForBinaryOperand, where it returns a string literal type for the RHS side if its a string literal, or its type instead.

All reactions

Sorry, something went wrong.

Uh oh!

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

@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Dec 22, 2015

Copy link
Copy Markdown
Member

Wesley Wigham (@weswigham) I think #6196 is the answer to your first TODO 😃

All reactions

Sorry, something went wrong.

Uh oh!

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

@weswigham Wesley Wigham (weswigham) mentioned this pull request Jan 21, 2016
Suggestion: sum types / tagged union #186
Closed
@weswigham Wesley Wigham (weswigham) mentioned this pull request Mar 11, 2016
make the strict equality comparison operator a typeguard for string literal types #7447
Closed
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Apr 5, 2016
String literal types #5185
Merged
@mquandalle

Maxime Quandalle (mquandalle) commented Apr 23, 2016

Copy link
Copy Markdown

Is this now fixed by #8010?

All reactions

Sorry, something went wrong.

Uh oh!

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

@basarat

Basarat Ali Syed (basarat) commented Apr 24, 2016

Copy link
Copy Markdown
Contributor

Is this now fixed by #8010?

Nope.

type Action = { type: "REQUEST" } 
            | { type: "SUCCESS", data: string } 
            | { type: "FAILURE", error: any }

let action: Action;
if (action.type === "REQUEST"){
  let inside = action; // Still `Action`
}

Tested with alm just to be sure 🌹 :)
image

All reactions

Sorry, something went wrong.

Uh oh!

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

@weswigham

Wesley Wigham (weswigham) commented Apr 24, 2016

Copy link
Copy Markdown
Member Author

I probably need to rework this PR, #8010 heavily modified the type guard code.

All reactions

Sorry, something went wrong.

Uh oh!

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

Wesley Wigham (weswigham) and others added 4 commits April 26, 2016 04:45
@weswigham
initial pass at equality as member type guards
b3db6ff
@weswigham
accept present baselines so output for new tests is captured
6e29b51
@weswigham
improve style/names
f88606a
@weswigham
Remove awful merge conflicts
74f3bd0
@weswigham
Wesley Wigham (weswigham) force-pushed the narrow-by-value branch from a87adee to 0e30aa1 Compare April 26, 2016 11:24
@weswigham
Reconcile member-guards with CFA work
6636b7b
@weswigham
Wesley Wigham (weswigham) force-pushed the narrow-by-value branch from 0e30aa1 to 6636b7b Compare April 26, 2016 11:28
@weswigham
This shouldnt have changed
dbe2a55
@weswigham

Wesley Wigham (weswigham) commented Apr 26, 2016 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

I think I've mostly reconciled the work in this PR with master and the control-flow based type guards therein. Additionally, the bug I had with the code in the emitter is gone now, so that's nice. It feels like it works more cleanly now that CFA-based guards are in. I've updated the OP to match current progress.

mquandalle, RReverser, zpdDG4gta8XKpMCd, michaelmesser, and Lenne231 reacted with hooray emoji
All reactions
  • 🎉 5 reactions

Sorry, something went wrong.

Uh oh!

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

@weswigham
Tentatively accept test baseline
bca9c33
@weswigham

Wesley Wigham (weswigham) commented Apr 26, 2016

Copy link
Copy Markdown
Member Author

Daniel Rosenwasser (@DanielRosenwasser) There's a change to the compatibility relationship test baselines (which have this kind of comparison in them) - I'm not 100% sure what the desired behavior there is, do you want to look at it?

All reactions

Sorry, something went wrong.

Uh oh!

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

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.

6 participants

@weswigham @DanielRosenwasser @mquandalle @basarat @Lenne231 @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.