V
V
veryoriginalnickname2021-11-27 16:34:04
typescript
veryoriginalnickname, 2021-11-27 16:34:04

How to query for a specific type in type union arguments?

How to make it so that in the interface 'B', the function '1' takes an argument of type 'A', whose name has the value '1', and so on?

type A = {
    name: '1'
} | {
    name: '2'
} | {
    name: '3'
}

interface B {
    1: (a: A) => void // 'A', где name 1
    2: (a: A) => void // 'A', где name 2
    3: (a: A) => void // 'A', где name 3
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WbICHA, 2021-11-27
@veryoriginalnickname

interface A<T extends number> {
  name: `${T}`;
}

A<1>
A<2>
...

-------------
https://www.typescriptlang.org/play?#code/JYOwLgpg...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question