W
W
webe2022-01-19 15:21:22
typescript
webe, 2022-01-19 15:21:22

Can TS get array values?

const names = ["иван", "антон"]
const fn = (name) => name

How to make it so that only Ivan and Anton can be sent to the function? (the data is in the array)
Or is it impossible in the vehicle?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex, 2022-01-19
@Kozack

const names = ["иван", "антон"] as const
const fn = (name: typeof names[0]) => name

https://www.typescriptlang.org/play?#code/MYewdgzg...

0
0xD34F, 2022-01-19
@0xD34F

const names = [ 'иван', 'антон' ] as const;
type Name = typeof names[number];


const fn = (name: Name) => name;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question