How do I coerce this function to accept multiple types then conditionally change the output?

Using traditional overloading approach doesn't seem to work as expected. Union types on parameters does not resolve the output type properly:
const convertInfluenceType = (type: InfluenceTypeEnum | number)
// Outputs InfluenceTypeEnum | 1 | 4 | 7
const convertInfluenceType = (type: InfluenceTypeEnum | number)
// Outputs InfluenceTypeEnum | 1 | 4 | 7
Error:
Type '(type: number | InfluenceTypeEnum) => InfluenceTypeEnum | 1 | 4 | 7' is not assignable to type 'ConvertFn'.
Type '(type: number | InfluenceTypeEnum) => InfluenceTypeEnum | 1 | 4 | 7' is not assignable to type '(type: number) => InfluenceTypeEnum'.
Type 'InfluenceTypeEnum | 1 | 4 | 7' is not assignable to type 'InfluenceTypeEnum'.
Type '1' is not assignable to type 'InfluenceTypeEnum'.
Type '(type: number | InfluenceTypeEnum) => InfluenceTypeEnum | 1 | 4 | 7' is not assignable to type 'ConvertFn'.
Type '(type: number | InfluenceTypeEnum) => InfluenceTypeEnum | 1 | 4 | 7' is not assignable to type '(type: number) => InfluenceTypeEnum'.
Type 'InfluenceTypeEnum | 1 | 4 | 7' is not assignable to type 'InfluenceTypeEnum'.
Type '1' is not assignable to type 'InfluenceTypeEnum'.
Solution:
i was looking around a bit and found this pattern with overloads
No description
Jump to solution
4 Replies
Fursum
Fursum2y ago
hmm if block resolves to T & number but return type doesnt accept the return
Fursum
Fursum2y ago
oh you cast it
Solution
Fursum
Fursum2y ago
i was looking around a bit and found this pattern with overloads
No description
Fursum
Fursum2y ago
seems like a better solution for my use case though i should be more familiar with extends uses, will definitely be useful thanks!
Want results from more Discord servers?
Add your server