0

I have following interface:

interface Point<T> {
  x: T;
  y: T;
}

Now I want to have an array of points:

let points = [
  { x: 1, y: 1 }, // this is ok
  { x: "a", y: "b" }, // also ok
  { x: 1, y: "str" }, // should be an error
];

What type would I give to the points variable? The only one I can think of is (Point<number> | Point<string>)[], but what if the types are not known ahead of time?

dnt
  • 113
  • 1
  • 5
  • 1
    why would you not no *if the types are not known ahead of time*. I'm pretty sure your over thinking whatever it is your trying to do. What you're trying to do breaks [Liskov substitution principle](https://en.wikipedia.org/wiki/Liskov_substitution_principle) so you shouldn't do it – Liam Jan 28 '21 at 16:11
  • Taking the answer to the other question and translating it here gives [this](https://tsplay.dev/PmLxam) code – jcalz Jan 28 '21 at 16:35

0 Answers0