M
M
miliko mikoyan2019-08-23 16:39:43
typescript
miliko mikoyan, 2019-08-23 16:39:43

How to determine from 2 types: 1 or 2?

I am new to typing.
I have 2 types.

type Point = {
    x: number;
    y: number;
};

type Label = {
    name: string;
};

I have a Duko variable.
let Duko: ? = {...};
How to say that Duko corresponds to a Label or Point type.
In other words, Duko can accept.
let Duko = {
  x: 0,
  y: 0,
}; 

или

let Duko = {
  name: "origin" 
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2019-08-23
@miliko0022

There is some crutch that can be dug out in the propositions of the typescript. Perhaps someday something like this will become part of the language. But this bullshit assumes that there can be no intersecting properties.
https://www.typescriptlang.org/play/index.html#cod...
In general, you just need to write in such a way that you don’t have to pervert like that. Make tipguards on the edge.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question