Skip to content

Cannot infer type in generic Readonly #14360

Closed
@anvish

Description

@anvish

TypeScript Version: 2.2.1

Code

interface A {
    b?(): void
}

function works<T extends A>(a: T) {
    if (a.b) a.b()
}

function worksToo(a: Readonly<A>) {
    if (a.b) a.b()
}

function doesntWork<T extends A>(a: Readonly<T>) {
    if (a.b) a.b()
    // Cannot invoke an expression whose type lacks a call signature.
    // Type '(() => void) | undefined' has no compatible call signatures.
}

Note that there is no Object is possibly 'undefined' message here.

Expected behavior:
After if check method should be considered defined.
Actual behavior:
Member type is not inferred correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions