Is is possible in the lastest TypeScript version to pass an object literal as argument to a function without widening it, and also without using as const in the invocation?
link to TS Playground: Example
What I currently do is this:
function func<T>(value: T): T {
return value;
};
let test = func({ key: 'value' })
// type inferred as { key: string;}
what I want is the following
// ... alternative declaration of func
let test = func({ key: 'value' })
// type inferred as { key: "value"; }
More precisely it should work for any object literal extending Record<string,string>
These archive the result I want, but Id like not to change the way the function must be invoked
function func<T>(value: T): T {
return value
};
let test = func({ key: 'value' as const })
// type inferred as { key: "value"; }
let test = func({ key: 'value' } as const )
// type inferred as { readonly key: "value"; }
Is this possible?
Yes, this is possible. But the solution might seem unintuitive and redundant.
You will have to add another generic type to the function. This will keep will allow us to keep narrowed type of string literals passed to the function.
We can apply this to your complex example.
Playground
Here are some further resources which helped me with problems like these in the past.
Typescript: Type Inference on function arguments
Suggestion: Const contexts for generic type inference - This post describes a way to narrow down object literals of arbitrary shape.