Skip to content
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

Assertion function error messages are unactionable and unclear #35838

Closed
DanielRosenwasser opened this issue Dec 24, 2019 · 1 comment · Fixed by #35839
Closed

Assertion function error messages are unactionable and unclear #35838

DanielRosenwasser opened this issue Dec 24, 2019 · 1 comment · Fixed by #35839
Assignees
Labels
Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements

Comments

@DanielRosenwasser
Copy link
Member

class Foo {
    bar?: Foo

    public setBar(): asserts this is Foo & {bar: Foo}  {
        this.bar = this;
    }

    public sayHi(): string {
        return 'Hi!';
    }
}

function test() {
    let foo = new Foo();

    foo.bar = undefined;

    foo.setBar();

    if (foo.bar) {
        console.log('This should be OK: ' + foo.bar.sayHi());
    }
}

Current error

Assertions require every name in the call target to be declared with an explicit type annotation.

'foo' is declared here.

foo is declared here but... but okay so what am I supposed to do now??

@DanielRosenwasser DanielRosenwasser added Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements labels Dec 24, 2019
@DanielRosenwasser DanielRosenwasser self-assigned this Dec 24, 2019
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 3.8.0 milestone Dec 24, 2019
@threehams
Copy link

Is it possible this would make arrow function assertion errors even more confusing, given their use it marked as a design limitation in #34523? This example would (I think?) result in the error constAssert requires an explicit type annotation, which is still unclear.

function funcAssert (condition: boolean): asserts condition {
    if (!condition) {
        throw new Error();
    }
}

const constAssert = (condition: boolean): asserts condition => {
    if (!condition) {
        throw new Error();
    }
}

(thing: number | null) => {
    funcAssert(!!thing); // this is fine
    thing;
}
(thing: number | null) => {
    constAssert(!!thing); // why doesn't this work, and what am I supposed to do?
    thing;
}

https://www.typescriptlang.org/play/?ssl=21&ssc=1&pln=1&pc=1#code/GYVwdgxgLglg9mABKSBBAzugpgJyogCggQBMZYEAuRAIzjgBssBDMASmuc1ynUWLBkKSAN4AoRJMQxghAIQCh8donFT1iKAAsccAO6IwWAwFEcunATYBuCVIC+Yx2IHp8rqBmx5EAXkKK5MrUdIws7JzceHyBwn4AfKp2kjLyscpsSRpS2roGRqbmcJY2yYiOzgTaMGAA5tRgIAC2NLiIAD6GIAwMmb6JalIoEF48BHJy1XU2iAD0s5paMHzLyDVYZVO1to5VS3UNza04HV09fQNlHqN445P7tTPziHpaAJ6IJHBY6GAA5Phqnw9MUANYAGkQrBILy0zHwzCaiAAkoh0CAAA4YuDYGFQOCfOAAfk2Dx2YiAA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants