Skip to content

Union with constrained type variable doesn't narrow in negative branch of type predicate #44814

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
saschaB91 opened this issue Jun 29, 2021 · 2 comments · Fixed by #59628
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@saschaB91
Copy link

⏯ Playground Link

https://www.typescriptlang.org/play?#code/LAKAxgNghgzjAEAVApjALgYWnAPIgfPAN6jxnxoCeADsgOYCuUATgCYAUAblBAFzwMAdgGtBAewDuggJT9uEeAEsEiYqXIbmyNA2aCKzBsgDc6sgF8z8AGZd+ghgFsARsmaz4nMYtbFLIDQALOyQPLx8-K2shMC4eflUAH3gHFzcw718SAI1yRWt4djRA5QA6KlpGFg55aWk1HNzc4rLg2tNGpvItHT0Orvh-AZaYUtt2q39-UEhYFVRMbBgAJjx4ZAAPNGRBVgQAEShtwmyNCvomNji+ARFxKTCeJRUGgZ7dfTRDE0mokNTXO45JlIp02gkMhEiENyNFBLF5Al4MkAelgVCrBp8oURuUaBdqtc6q8BuRcW0eNJ+qSyO8+pjyDCmrjxpTqRZQOYgA

Issue Type: Bug

This error is only shown when using the TypeScript-Server not when compiling code.
When using an generic class with a method as type guard, it will only be validated correctly if you do not extend the generic type.
The first class is ok but the second shows an error on "this.f(val)" even though nothing changed

class TestClass<T> {
    typeguard(val: unknown): val is T {
        return true;
    }
    f(v: number): void {}
    h(v: T): void {}
    func(val: T | number): void {
        if (this.typeguard(val)) {
            this.h(val);
            return;
        }
        this.f(val);
    }
}
class TestClass2<T extends Date> {
    typeguard(val: unknown): val is T {
        return true;
    }
    f(v: number): void {}
    h(v: T): void {}
    func(val: T | number): void {
        if (this.typeguard(val)) {
            this.h(val);
            return;
        }
        this.f(val);
    }
}
@MartinJohns
Copy link
Contributor

This error is only shown when using the TypeScript-Server not when compiling code.

This is not true. The error is shown when compiling just fine.

image

This is possibly a duplicate of #42185.

@andrewbranch andrewbranch changed the title Typescript Server not validating userdefinded type guards correctly Union with constrained type variable doesn't narrow in negative branch of type predicate Jun 30, 2021
@andrewbranch andrewbranch added the Bug A bug in TypeScript label Jun 30, 2021
@andrewbranch
Copy link
Member

Looks like a regression between 4.2 and 4.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants