typescript destructuring assignment with interface

wgf4242

I tried to destructuring assignment with interface, but cannot write like this.

interface TYPE {
  id?: number;
  type?: string;
}

const e =  {
  'id': 123,
  'type': 'type_x',
  'other': 'other_x'
}
const {...foo}: {foo: TYPE} = e;
console.log(foo.id, foo.type) // expected: 123, 'type_x'
Jack Bashford

Just declare the type on the variable, without that weird object notation:

const { ...foo }: TYPE = e;

That is a weird way to make a copy of an object however - it's usually done like so:

const foo: TYPE = { ...e };

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Destructuring assignment in Typescript

Destructuring assignment in Typescript for global variables

Destructuring assignment via TypeScript in React

Syntax Error on Destructuring Assignment in TypeScript

Angular 7: destructuring with Typescript interface, getting errors

Typescript generic interface parameter assignment

Typescript Object destructuring results in "Property assignment expected."

TypeScript type modification when using defaults and object destructuring assignment

Property does not exists on type when destructuring assignment and 'Pick' in Typescript

TypeScript: how to remap interface on 1 element while destructuring object?

How to namespace Destructuring assignment?

Is this an example of destructuring assignment?

Destructuring assignment, but without a pattern

Python assignment destructuring

Object property assignment with destructuring?

Destructuring assignment for nested properties

Destructuring assignment and variable swapping

Destructuring assignment in object creation

Destructuring assignment and null coalescing

JavaScript Destructuring Assignment

JS Proxy & destructuring assignment

Must use destructuring props assignment (react/destructuring-assignment)

Must use destructuring theme assignment react/destructuring-assignment

Destructuring assignment with rename and typing information

Strange destructuring assignment, giving undefined

What is destructuring assignment and its uses?

Why is a destructuring augmented assignment not possible?

how to bind data to this by destructuring assignment?

ES6 Destructuring assignment with `this`