1

I don't understand why the compiler says, in function test1, that property "name" and "surname" don't exist on type "ITest1; I'm confusing...:

interface ITest1{
    name: string;
    surname: string;
    age: number;
}

interface ITest2{
    city: string;
    nation: string;
    isEU: boolean;
}


//function test1<ITest1, ITest2>(a:ITest1|ITest2): ITest1|ITest2{
    function test1<ITest1, ITest2>(a:ITest1|ITest2): string{
   return (a as ITest1).name +
   (a as ITest1).surname;

}

let a : ITest1 = {
    name:"",
    surname:"",
    age:0
};
a.name="John";
a.surname="Taylor";
a.age=30;

console.log(test1(a));

1
  • And what if someone calls test1({city: "", nation: "", isEU: false})? Why does test1 accept an ITest1 | ITest2 parameter? Commented Aug 26, 2019 at 19:29

1 Answer 1

1

You need to fix your function (though I am not sure what you want to achieve with <ITest1, ITest2>). Just change it to:

function test1(a: ITest1 | ITest2): string {
  return (a as ITest1).name + (a as ITest1).surname;
}

Output:

$ tsc index.ts && node index
$ JohnTaylor

Edit:

Test for a certain property to differ ITest1 and ITest2:

function test1(a: ITest1 | ITest2 ): string {
  if ('name' in a) {
    // a implements ITest1
    return `${a.name} ${a.surname}`;
  }
  // a implements ITest2
  return `${a.city} ${a.nation}`;

  // Or just:
  // return 'name' in a ? `${a.name} ${a.surname}` : `${a.city} ${a.nation}`;
}

...

console.log(test1(a));
console.log(test1({city: "Foo", nation: "Bar", isEU: false}));

Result:

John Taylor
Foo Bar
Sign up to request clarification or add additional context in comments.

2 Comments

And when someone calls test1({city: "", nation: "", isEU: false})?
@jcalz Good point and done. I hope it's the right way of doing so :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.