0

I have a 2-field object like this

{ id: number, name: string }

...and want to remove the name item.

How do I accomplish that in TypeScript? I've tried using filter, delete etc but all I get is

TS2339: Property 'filter' does not exist on type '{}'.

I think this relates to this question What is "type '{}'"? but I need help to figure it out.

langkilde
  • 1,473
  • 1
  • 20
  • 37

1 Answers1

1

Consider :

let x : { id: number, name?: string } = {id: 1, name: 'foo'};

You can simply use delete:

delete x.name;
console.log(x); // {id: 1}
basarat
  • 261,912
  • 58
  • 460
  • 511
  • Thanks, that works! This lead me to a new question: https://stackoverflow.com/questions/45230325/how-to-type-redux-state-in-typescript-to-create-new-state-without-item – langkilde Jul 21 '17 at 06:17