Skip to content

Instance methods on inferred type instances which return never don't trigger unreachable codeΒ #50363

Closed
@claycoleman

Description

@claycoleman

Bug Report

πŸ”Ž Search Terms

return never instance method unreachable code

πŸ•— Version & Regression Information

Saw this first on version 4.4.4, confirmed still exists in 4.7.4

⏯ Playground Link

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABFAFgJzgdwM4AoCUAXImAKYBupaiA3gFCKPLpYmmaICiaGaBdAXzp1QkWAmSlsUAKpg0pAIYQUigEYAbUgEYCtBk1QYcBANwHGFxAHprybImAbFAcweoYDxQ-ALlqzVIrCARsOC0AOg04F1wAIk9EXyUVdS0AQjj8QWFciGdsBwAVKSgAYQKHeiZmYzwiNkpqaprDFg4yDm5efhqhIToQsGlEEukK72wASWGoRUhSRABeNg4x8sr+EXBoeCQoUrk-VMCAJj0WxnWJwpnpeYhSCKMsevMrW0QAPh+f+0QAFQAgAmcCkJDgUCBjmcLmYiW8SXkKQCWmCoXCT2isTi2BQcBAGmBiDUi2S-jSi0I8WyAiAA

πŸ’» Code

function throws(): never {
    throw new Error()
}

function testUnreachable1() {
    throws();
    
    // ts flags this as unreachable
    console.log("is unreachable!")
}




class TestClass {
    throws(): never {
        throw new Error()
    }
}

const TestClassInstance = new TestClass()

function testUnreachable2() {
    TestClassInstance.throws();

    // >>>>> ts **does not** flag this as unreachable
    console.log("should be unreachable :(")
}

πŸ™ Actual behavior

Unlike a stand-alone function, instance methods which return never don't mark the following lines in the block as unreachable code

πŸ™‚ Expected behavior

Similar to a stand-alone function, instance methods which return never should mark the following lines in the block as unreachable code

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions