1

Пример типа:

type MyStringNumber = '10'

Необходимо так:

type MyNumber = StringToNumber<MyStringNumber> // -> number

Как можно реализовать такой тип?

1 ответ 1

2

Можно так, используя ограничения и infer:

type StringToNumber<S extends `${number | bigint}`> = 
  S extends `${infer N extends number | bigint}`
    ? N
    : never;

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.