Skip to content

TS doesn't check the implementation of the overloaded function whether match its signature. #18533

Closed
@zheeeng

Description

@zheeeng

TypeScript Version: 2.5.2

Code

TS only check the function signature whether is compatible with that overloaded function's signature. If not, TS report Overload signature is not compatible with function implementation.. It actually doesn't check the function's implementation, only the signature's compatibility. In the example below:

class Animal { }
class Dog extends Animal { }

function test(d: Dog): string
function test(d: Animal): number
function test(d: Animal | Dog): number | string {
    if (d instanceof Dog) {
        return 42
    } else {
        return '苟'
    }
}

If we call test as:

const o = test(new Dog())

o is inferred as the string type, but actually, it's number type, eventually, cause the runtime error.

Expected behavior:

Report the first and second test function doesn't be implemented. The third test function signature pass. TS should be able to check the implementation with these signatures which are overloaded, we have done the type narrowing.

Actual behavior:

no errors

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