Skip to content

Issue deriving tuple types on overridden methodΒ #57387

Closed as not planned
Closed as not planned
@pvande

Description

@pvande

πŸ”Ž Search Terms

tuple type infer inference inheritance

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about tuples and type inference.

⏯ Playground Link

https://www.typescriptlang.org/play?alwaysStrict=false&target=2&jsx=0&module=5&ts=5.3.3&filetype=ts#code/CYUwxgNghgTiAEAXAngBwQZUTAlgOwHMAxHGAZ0QBUBXVCBAXngG0LdCAaeAOl6j2QBdANwAoUZChky8IgHs58AN6j4a+GXRgAFAEoAXPCztipCjToIV6m-DiJqMPCwBEACxAQIcl4NXqAX1EgiWhpeAAhWHgQAA9EEDxgGXlFa3VNcD1lf1s7EAcnVwIFYAAjZBAXLiUyOQBbArd8AkMXTzIqgL8bIICgA

πŸ’» Code

declare type StringFirstTuple = [string, ...any];

class Foo {
    spec(): StringFirstTuple {
        return ["hello"]
    }
}

class Bar extends Foo {
    spec() {
        return ["goodbye", {something: "else"}]
    }
}

πŸ™ Actual behavior

The spec method on Bar failed to typecheck, despite unambiguously implementing the same type signature as spec on Foo.

Annotating Bar's spec method to return a StringFirstTuple typechecks properly.

The error message seems to indicate that the return type of spec in Bar has been generalized to Array<string | {something: string}> before checking the overridden type.

πŸ™‚ Expected behavior

A function that returns an array that is hard-coded to match a tuple type should be substitutable for a function that is specified to return that tuple type.

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions