In TypeScript, can I specify the type of the local variables created using the object rest/spread assignment feature? The problem is that the ES6 syntax obj: Type
normally used by TypeScript to specify the type of a local variable is used in ES6 rest/spread definitions to refer to the name of the local variable(s) being defined if the name(s) are different from the object's property name(s). So how can I let TS know about the type that I'd like that variable to be?
For example, assume this code:
interface ABCD {
a: string,
b: number,
c: string,
d: number,
};
interface CD {
c: string,
d: number,
};
const abcd: ABCD = {a: 'hello', b: 10, c: 'world', d: 20};
const {a, b, ...cd} = abcd;
Now if I wanted to specify that the variable cd
should be type CD
, how would I do it? The normal TS syntax for type specification below would define a new local variable CD
.
const {a, b, ...cd: CD} = abcd;