'Typecasting when render the prop with union Types
I have a two properties in my component.
prop1: CustomObject<string, type1>
prop2: CustomObject<string, type2> | Array I will accept only prop1 on priority and if prop1 is not available i will look for prop2. So i have created union type like the below to pass the other methods as type to make it simple.
type inputType= type1 | type2
So in render method i am accessing the prop like this but it is accepting only any as type.
const propsData: any = this.props.prop1 ?? this.props.prop2
This statement is working but i don't want to give any
const propsData: CustomObject<string | InputType> | Array<string> = this.props.prop1 ?? this.props.prop2
the error i am getting in IDE is
Type 'String[] | CustomObject<string, type1> | CustomObject<string, type2>' is not assignable to type 'CustomObject<string, inputType> | String[]'.
How to handle this?
Solution 1:[1]
Looks like CustomObject expects 2 arguments, you may want to try below code as per error message.
const propsData: CustomObject<string, type1> | CustomObject<string, type2> | Array<string> = this.props.prop1 ?? this.props.prop2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dhaker |
