Skip to content

this types in intrinsic class attributes not inferred correctly #21662

Open

Description

Code

// @strict: true
// @jsx: preserve
namespace JSX {
    export interface Element {}
    export interface IntrinsicClassAttributes<TClass> {
        ref?: (ref: TClass) => void;
        acceptProps?: (props: this) => boolean;
        key: string;
    }
    export interface ElementClass extends Element {}
    export interface ElementAttributesProperty { props: {}; }
    export interface ElementChildrenAttribute { children: {}; }
    export interface IntrinsicAttributes {}
    export interface IntrinsicElements { [key: string]: Element }
}
class ElemClass<T extends {x: number}> implements JSX.ElementClass {
    constructor(public props: T) {}
}
const elem = <ElemClass x={12} y={24} key="elem" ref={me => void me.props.y} acceptProps={p => p.ref(null as any) || void p.key || void p.y} />

Expected behavior:
no errors

Actual behavior:

    const elem = <ElemClass x={12} y={24} key="elem" ref={me => void me.props.y} acceptProps={p => p.ref(null as any) || void p.key || void p.y} />
                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2322: Type '{ x: number; y: number; key: string; ref: (me: ElemClass<any>) => undefined; acceptProps: (p: IntrinsicAttributes & IntrinsicClassAttributes<ElemClass<any>> & any) => undefined; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<ElemClass<{ x: number; y: number; key: string; ref: {}; acceptProps: {}; }>> & { x: number; y: number; key: string; ref: {}; acceptProps: {}; }'.
!!! error TS2322:   Type '{ x: number; y: number; key: string; ref: (me: ElemClass<any>) => undefined; acceptProps: (p: IntrinsicAttributes & IntrinsicClassAttributes<ElemClass<any>> & any) => undefined; }' is not assignable to type 'IntrinsicClassAttributes<ElemClass<{ x: number; y: number; key: string; ref: {}; acceptProps: {}; }>>'.
!!! error TS2322:     Types of property 'acceptProps' are incompatible.
!!! error TS2322:       Type '(p: IntrinsicAttributes & IntrinsicClassAttributes<ElemClass<{ x: number; y: number; key: string; ref: {}; acceptProps: {}; }>> & { x: number; y: number; key: string; ref: {}; acceptProps: {}; }) => undefined' is not assignable to type '((props: IntrinsicClassAttributes<ElemClass<{ x: number; y: number; key: string; ref: {}; acceptProps: {}; }>>) => boolean) | undefined'.
!!! error TS2322:         Type '(p: IntrinsicAttributes & IntrinsicClassAttributes<ElemClass<{ x: number; y: number; key: string; ref: {}; acceptProps: {}; }>> & { x: number; y: number; key: string; ref: {}; acceptProps: {}; }) => undefined' is not assignable to type '(props: IntrinsicClassAttributes<ElemClass<{ x: number; y: number; key: string; ref: {}; acceptProps: {}; }>>) => boolean'.
!!! error TS2322:           Types of parameters 'p' and 'props' are incompatible.
!!! error TS2322:             Type 'IntrinsicClassAttributes<ElemClass<{ x: number; y: number; key: string; ref: {}; acceptProps: {}; }>>' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<ElemClass<{ x: number; y: number; key: string; ref: {}; acceptProps: {}; }>> & { x: number; y: number; key: string; ref: {}; acceptProps: {}; }'.
!!! error TS2322:               Type 'IntrinsicClassAttributes<ElemClass<{ x: number; y: number; key: string; ref: {}; acceptProps: {}; }>>' is not assignable to type '{ x: number; y: number; key: string; ref: {}; acceptProps: {}; }'.
!!! error TS2322:                 Property 'x' is missing in type 'IntrinsicClassAttributes<ElemClass<{ x: number; y: number; key: string; ref: {}; acceptProps: {}; }>>'.

Unlike I initially expected, there's only an issue with this types - this types are formulated from contextual types, so fixing this probably just requires tightening the new jsx inference code to instantiate the type with the correct this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: JSX/TSXRelates to the JSX parser and emitter

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions