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

Cannot infer type in generic Readonly #14360

Closed
anvish opened this issue Feb 28, 2017 · 3 comments
Closed

Cannot infer type in generic Readonly #14360

anvish opened this issue Feb 28, 2017 · 3 comments
Labels
Fixed A PR has been merged for this issue

Comments

@anvish
Copy link

anvish commented Feb 28, 2017

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.

@ericanderson
Copy link
Contributor

ericanderson commented Mar 21, 2017

Hey: @ahejlsberg @yuit @DanielRosenwasser @mhegazy

This is biting us pretty hard on the React typings for DefinitelyTyped. Here is another example:

interface Props {
    callback?: (v: number) => void;
}

class Component<P> {
    protected propsReadonly: Readonly<P>;
    protected propsWritable: P;
}

abstract class AbstractComponent<P extends Props> extends Component<P> {
    public doStuff() {
        const callbackReadonly = this.propsReadonly.callback; // (P & { children: Component<any>[]; })["callback"]
        const callbackWritable = this.propsWritable.callback; // ((v: number) => void) | undefined
        if (callbackReadonly) {
            callbackReadonly(5); // ERR: Cannot invoke an expression whose type lacks a call signature. Type '((v: number) => void) | undefined' has no compatible call signatures.
        }

        if (callbackWritable) {
            callbackWritable(5); // OKAY
        }
    }
}

class OtherComponent extends Component<Props> {
    public doStuff() {
        const callbackReadonly = this.propsReadonly.callback;
        if (callbackReadonly) {
            callbackReadonly(5); // OKAY
        }
    }
}

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 24, 2017
@mhegazy
Copy link
Contributor

mhegazy commented May 26, 2017

This should be working as expected in latest. fixed by #15576

@mhegazy mhegazy closed this as completed May 26, 2017
@mhegazy mhegazy added Fixed A PR has been merged for this issue and removed Needs Investigation This issue needs a team member to investigate its status. labels May 26, 2017
@ericanderson
Copy link
Contributor

Thank you

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants