Issue
To put it simple, I have an interface A:
interface A { names: string[]; nr: number; }
And I can pass/create an A object so:
{ names: ['John'], nr: 11 }
However, how could I pass it without writing the keys, so like below:
{ ['John'], 11 }
Is it possible in typescript?
Solution
Seems like you are looking for a tuple type:
type A = [name: string[], nr: number];
const example: A = [['John'], 11];
Here's an example TS playground link.
Edit: keyed arrays aren't really a thing in TS/JS, but perhaps you could use a function to get the desired results?
interface A { names: string[]; nr: number; }
function makeA (names: A["names"], nr: A["nr"]): A {
return { names, nr };
}
const a: A = makeA(['John'], 11);
console.log(a.name, a.nr);
Here's the TS playground link.
Answered By - Nick
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.