I have tried this case:
let a = { id: 1, name: "Misha" };
function b(id: number, name: string) {
}
b(...a);
I need that all properties of object will be applied as parameters
I have tried this case:
let a = { id: 1, name: "Misha" };
function b(id: number, name: string) {
}
b(...a);
I need that all properties of object will be applied as parameters
TypeScript doesn't support spreading objects to parameter names.
If it's possible for you to change the function signature though, you can expect an object of a compatible type as the first function parameter, like this:
// interface name is just an example
interface IUser {
id: number;
name: string;
}
let a: IUser = { id: 1, name: "Misha" };
function b({ id, name }: IUser) {
console.log(`User ID is ${id} and name is "${name}"`);
}
b(a);
like this.
function NewFunction(...[foo, bar]: Parameters<typeof OtherFunction>){}
function OtherFunction(enabled?: boolean; message?: string) {}
TypeScript does support spreading objects to parameter names.
Write the three dots before the parameter name, and an array operator next to the type.
Example :
function add(x: number, ...vals: number[])
© 2022 - 2024 — McMap. All rights reserved.