I have this:
interface Obj {
foo: string,
bar: number,
baz: boolean
}
The desired type is this tuple:
[string, number, boolean]
How can I convert the interface to the tuple?
Update:
My original problem is: I make some opinionated library with a declarative spirit, where a user should describe parameters of a function in an object literal. Like this:
let paramsDeclaration = {
param1: {
value: REQUIRED<string>(),
shape: (v) => typeof v === 'string' && v.length < 10
},
param2: {
value: OPTIONAL<number>(),
...
},
}
Then the library takes this object and creates a function with parameters from it:
(param1: string, param2?: number) => ...
So, making such function is not a problem, the problem is to correctly type it, so that user gets good code-completion (IntelliSense).
P.S. I know it's not solvable, but it would be interesting to know what is the closest possible workaround/hack.
paramsDeclaration
as an array instead of an object? If you want names for the parameters, you can put them in fields of the sub-objects. – DeasonparamsDeclaration
should be 0, 1 instead of param1, param2 ? I am considering this at the moment, its a plan B. – RaeparamsDeclaration
is in a declarative, readable form to display params, their names, and their other different traits, and having string keys is more readable/meaningful etc. – RaeparamsDeclaration = [{name: 'param1', value: REQUIRED<string>(), shape: ...}, {name: 'param2', ...}]
. It's a little clunker than your current syntax, but the names are still clearly visible. – Deason