1

What is the smartest way to convert:

export interface A {
    Id: number;
    Name: string;
    Surname: string;
    Age: number;
}

export interface B {  //<---EDITED (at first i wrote "A"- sorry guys
    Id: number;
    Name: string;
    Surname: string;
    City: string;
}


let source :A = {
    Id = 1
    Name = "aaaa"
    Suramne = "bbbb"
    Age = 99
}

to:

{
    Id = 1
    Name = "aaaa"
    Suramne = "bbbb"
    City = ""
}

a sort of downcast, mapping only the existing properties (with the same name) and lost the others are missing in the target object.

pinale
  • 2,060
  • 6
  • 38
  • 72
  • 3
    As types, values, or both? There seems to be some invalid syntax in the code snippets provided, too. – kelsny Mar 18 '22 at 13:38
  • Please provide a [mre] that clearly demonstrates the issue you are facing. Ideally someone could paste the code into a standalone IDE like [The TypeScript Playground (link here!)](https://tsplay.dev/WG5aKN) and immediately get to work solving the problem without first needing to re-create it. So there should be no pseudocode, typos, unrelated errors, or undeclared types or values. – jcalz Mar 18 '22 at 13:50
  • if you have the two interfaces called `A` in the same file then you have [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html) (meaning your variable is missing a property there). If they are in different files you can only ever have only one with that name. There's no automated tool for correct type casting of unrelated types you need to write your own conversion function – apokryfos Mar 18 '22 at 13:51
  • @apokryfos for all we know that's just a typo and it's really meant to be two different interfaces? Anyway alex you would do it the same way you do it in JavaScript, by manipulating the object yourself. Possible duplicate of https://stackoverflow.com/questions/31829951/how-to-reduce-javascript-object-to-only-contain-properties-from-interface – jcalz Mar 18 '22 at 13:56
  • sorry guys, i corrected the snippet, obviously they are two different interfaces – pinale Mar 21 '22 at 08:38

1 Answers1

3
interface A {
    Id: number;
    Name: string;
    Surname: string;
    Age: number;
}

interface B {
    Id: number;
    Name: string;
    Surname: string;
    City: string;
}


let source : A = {
    Id: 1,
    Name: "aaaa",
    Surname: "bbbb",
    Age: 99
}

let transformed = source as unknown as B;
console.log(transformed.Id) //1
console.log(transformed.Name) //"aaaa"
console.log(transformed.Surname) //"bbbb"
console.log(transformed.City) //undefined
console.log(transformed.Age) //99 But also Type Error!!

Note that transformed.Age still contains data, it will just also throw a type error and complain in the IDE.

It will however allow you to pass the object to a parameter requiring type B.

If you were to cast it back to A, you would still be able to access the data in Age.

This is not not very type safe. It would be better to create a method that maps it from one type to another, this way you can insure type safety the entire time

let TransformAToB = (input: A): B => {
    return {
        Id: input.Id,
        Name: input.Name,
        Surname: input.Surname,
        City: ""
    }
}

let transformed2 = TransformAToB(source)
console.log(transformed.Id) //1
console.log(transformed.Name) //"aaaa"
console.log(transformed.Surname) //"bbbb"
console.log(transformed.City) //""
Alex
  • 375
  • 3
  • 13
  • `let transformed = source as unknown as B;` it is the the quick and dirty trick i was looking for! thanks – pinale Mar 21 '22 at 14:07