首页 > 其他分享 >[Typescript] 102. Hard - String to Number

[Typescript] 102. Hard - String to Number

时间:2022-11-15 00:56:57浏览次数:52  
标签:ACC Typescript _____________ Number number Expect 102 type

Convert a string literal to a number, which behaves like Number.parseInt.

 

/* _____________ Your Code Here _____________ */

type ToNumber<S extends string, ACC extends unknown[] = []> = S extends `${number}`
  ? S extends `${ACC['length']}`
    ? ACC['length']
    : ToNumber<S, [...ACC, unknown]>
  : never


/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<ToNumber<'0'>, 0>>,
  Expect<Equal<ToNumber<'5'>, 5>>,
  Expect<Equal<ToNumber<'12'>, 12>>,
  Expect<Equal<ToNumber<'27'>, 27>>,
  Expect<Equal<ToNumber<'18@7_$%'>, never>>,
]

 

标签:ACC,Typescript,_____________,Number,number,Expect,102,type
From: https://www.cnblogs.com/Answer1215/p/16891093.html

相关文章