Skip to content

[bug/generics]: Class Generics doesn't flow properly to subclass generics #25296

@Hotell

Description

@Hotell

TypeScript Version: 2.9.2 + {strict:true}

Search Terms:
jsx generics, class generics

I wanna create React generic component. but the generic annotations flow doesn't work properly/is acting strange

Code

There are various issues with generics flow:

1. When I define Props member, which is type of function via property, following throws error:

image

[ts]
Argument of type 'Readonly<{ children?: ReactNode; }> & Readonly<Props<T>>' is not assignable to parameter of type 'Props<{}>'.
  Types of property 'onSelect' are incompatible.
    Type '(item: T, event?: SyntheticEvent<HTMLElement> | undefined) => void' is not assignable to type '(item: {}, event?: SyntheticEvent<HTMLElement> | undefined) => void'.
      Types of parameters 'item' and 'item' are incompatible.
        Type '{}' is not assignable to type 'T'.

but if i change it to "method" definition, error is gone

image

2. Generics default values doesn't flow properly:

image

throws

image

T = {} is not propagated correctly to Props and because of that we have those errors.

What partially helps is to constraining generic to string in this case, but event with that it won't flow to our Props['onSelect']

image

throws:

image

finally what fixes the issue is to cast value propagated to onSelect to our T

image

Expected behavior:

class generics should properly flow to subclasses generics

type Props<T extends string = string> = {
  items: T[]
  name: string
  onSelect(item: T, event?: SyntheticEvent<HTMLElement>): void
}
export class Select<T extends string = string> extends Component<Props<T>> {
  private readonly handleChange = (event: React.FormEvent<HTMLInputElement>) => {
    const { value, name } = event.currentTarget
    const isValueValid = this.props.items.find((item) => item === value)

    if (isValueValid) {
      this.props.onSelect(value)
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions