Skip to content

Error message for type guards are vague about rules #6540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DanielRosenwasser opened this issue Jan 19, 2016 · 4 comments
Closed

Error message for type guards are vague about rules #6540

DanielRosenwasser opened this issue Jan 19, 2016 · 4 comments
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Fixed A PR has been merged for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this

Comments

@DanielRosenwasser
Copy link
Member

From #6538

interface Animal {
    animal;
}

interface Dog extends Animal {
    dog;
}

interface Car {
    car;
}

let thing: Dog | Car;

function isAnimal(x: Car | Dog): x is Animal {
    return "animal" in x;
}

Currently we just report

Type 'Animal' is not assignable to 'Car | Dog'.
    etc.

But that should be the elaboration. The first thing the user sees should be something like

A type guard's type must be assignable to its parameter's type.`
    Type 'Animal' is not assignable to 'Car | Dog'.
        etc.

@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Help Wanted You can do this Good First Issue Well scoped, documented and has the green light Domain: Error Messages The issue relates to error messaging labels Jan 19, 2016
@DanielRosenwasser
Copy link
Member Author

This is a really easy bug to fix for anyone who wants to take a stab at this.

@mhegazy mhegazy added this to the Community milestone Feb 24, 2016
@Vinatorul
Copy link
Contributor

I am trying to do this. It was not hard to add new diagnostic message, but I am not sure where should I check that I am checking the assignability of the guarded type. Is there a special way to find it out like backtrace?

@DanielRosenwasser
Copy link
Member Author

checkTypePredicate is where the magic happens. Specifically, this has to do with the call to checkTypeAssignableTo. You'll probably want to use chainDiagnosticMessages to create a DiagnosticMessageChain.

@DanielRosenwasser
Copy link
Member Author

Thanks @Vinatorul!

@mhegazy mhegazy modified the milestones: TypeScript 2.0, Community Mar 3, 2016
@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.
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Fixed A PR has been merged for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

3 participants