0

I am converting an object one to other type in typescript. But the returned object has properties of original object. I want to get OutputInterface format, not with InputInterface.

export interface InputInterface {
  username: string;
  createdAt: number;
  active: boolean;
  roles: any;
}

export interface OutputInterface {
  username: string;
  active: number;
  roles: any;
}

const i: InputInterface = {
  username: "username",
  createdAt: 1,
  active: true,
  roles: []
};

const converted = (i as unknown) as OutputInterface;
console.log("final value", converted);

// final value { username: 'username', createdAt: 1, active: true, roles: [] }
Vinoth Rajendran
  • 1,181
  • 1
  • 13
  • 28
  • 2
    Typecasting in TypeScript doesn't affect the actual object at runtime, just the type information associated with it at compile-time. – Marty Feb 28 '20 at 03:38
  • You want to remove properties from the object when you cast it? Typescript doesn't affect the generated JavaScript, casting never actually changes the shape of an object, it's just you telling the compiler that you know what you are doing. You need to write a function to convert it yourself – Ruan Mendes Feb 28 '20 at 03:39

0 Answers0