-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
Description
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:
[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
2. Generics default values doesn't flow properly:
throws
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']
throws:
finally what fixes the issue is to cast value propagated to onSelect
to our T
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
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug