跳至主要內容

529. Absolute

鸭梨小于 1 分钟

529. Absolute

<题目描述>

::: playground#ts 529. Absolute

@file index.ts

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

type cases = [
  Expect<Equal<Absolute<0>, '0'>>,
  Expect<Equal<Absolute<-0>, '0'>>,
  Expect<Equal<Absolute<10>, '10'>>,
  Expect<Equal<Absolute<-5>, '5'>>,
  Expect<Equal<Absolute<'0'>, '0'>>,
  Expect<Equal<Absolute<'-0'>, '0'>>,
  Expect<Equal<Absolute<'10'>, '10'>>,
  Expect<Equal<Absolute<'-5'>, '5'>>,
  Expect<Equal<Absolute<-1_000_000n>, '1000000'>>,
  Expect<Equal<Absolute<9_999n>, '9999'>>,
]

// ============= Your Code Here =============
type Absolute<T> = any

:::

点击查看答案
type Absolute<
  T extends number | string | bigint
> = `${T}` extends `-${infer Rest}` ?
  Rest : `${T}`