• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: React + Typescript: How can I dynamically set a generic interface for a function?

Resolved: React + Typescript: How can I dynamically set a generic interface for a function?

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

Consider the following components:
Basic.tsx
Extended.tsx
I need the onPropertySelect’s (in Basic.tsx) type to equal whatever type is passed into it in Extended.tsx. i.e. onPropertySelect in Basic should know the type to use is (property: BasicPropertyDetails<ExtendedPropertyDetails>) => void (based on the value in Extended.tsx).
Note: The type of property inside onPropertySelect will have the same subtype as BasicPropertyDetails<unknown>
As of now I cannot get the above to work and the implementation (or the return tsx) in Extended throws an error for types not matching.

Answer:

In Extended.tsx, pass type T to onPropertySelect property.

If you have better answer, please add a comment about this, thank you!

javascript node.js reactjs typescript
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: I am facing ERR_HTTP2_PROTOCOL_ERROR on my website

27/03/2023

Resolved: TypeScript does not recognize properties when instantiating interface array

27/03/2023

Resolved: How to make statement case insensitive when uploading a dta file with specified columns?

27/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.