'Typescript stop object type from widening generic [duplicate]
const a =<T extends unknown>(arg:T)=>arg
const a_ = a(1) // type is 1
const b =<T extends unknown>(arg:T)=>({arg})
const b_= b(1) // type is {arg: number}, expect {arg: 1}
const c =<T extends unknown>(arg:T)=>({arg})
const c_= b(1 as const) // type is {arg: 1}, ok but degrade dev experience
In the example, if I return an object where the member type is based on the generic, it is widen back to number even if the generic type is 1
problem solved if I give it const assertion, but it is not a good dev experience
How can I stop this widening behaviour?
Note: T must extends unknown
Solution 1:[1]
Just add the type to the function call
const c = <T extends unknown>(arg: T) => ({arg})
const d: {arg: 1} = c<1>(1)
console.log(d.arg) // 1
In this way TS does not deduce the type from the argument (number in this case), it will use the type you give to him (<1>)
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 |
