Skip to content

#51865 breaks contextual type from ThisType in a call #51970

Closed
@sandersn

Description

@sandersn

Simplified from @types/moonjs:

type ConstructorOptions<Data> =
    & ComponentOptionsProperties<Data>
    & ThisType<Instance<Data>>;

interface ComponentOptionsProperties<Data> {
    data: Data;
    render(): unknown;
}

interface Instance<Data> {
    get<K extends keyof Data>(name: K): unknown;
}
declare var Moon: {
    <Data>(options?: ConstructorOptions<Data>): Instance<Data>;
};
const r2 = Moon({
    data: { msg: "" },
    render() {
        const h = (x: unknown) => x
        return h(this.get("msg")) // <---- error here
    },
})

Expected: No error on this.get("msg")
Actual: "'string' is not assignable to parameter of type 'keyof Data'."

"msg" should get the contextual type "msg", but doesn't. But Inference works correctly, so string isn't assignable to "msg"

This looks like a pretty standard use of ThisType to make a Vue-like virtual DOM. I couldn't get the failure to reproduce with the containing call to h as well as return.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueRecent RegressionThis is a new regression just found in the last major/minor version of TypeScript.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions