00529-medium-absolute

Back

type Absolute<T extends number | string | bigint> = T extends `-${infer StringNumber}` ? StringNumber
  : T extends number | bigint ? Absolute<`${T}`> : T

Solution by eunsukimme #35238

type Absolute<T extends number | string | bigint> = T extends `${infer S}${infer R}` ? S extends "-" ? R : T : Absolute<`${T}`>

Solution by ottantatre #35231

// your answers
type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer N}` ? N : `${T}`;

Solution by dev-jaemin #35103

// 你的答案
type Absolute<T extends string | number | bigint> = T extends string
  ? T extends `-${infer S}`
    ? S
    : T
  : Absolute<`${T}`>;

Solution by shx123qwe #34969

type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer Rest}` ? Absolute<Rest> : `${T}`

Solution by devshinthant #34608

type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer N}` ? `${N}` : `${T}`

a simple question requiring template knowledge

Solution by Mantou1233 #34511

type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer R}` ? R : `${T}`

Solution by ouzexi #34017

529 - Absolute

by Andrey Krasovsky (@bre30kra69cs) #medium #math #template-literal

Question

Implement the Absolute type. A type that take string, number or bigint. The output should be a positive number string

For example

type Test = -100
type Result = Absolute<Test> // expected to be "100"

View on GitHub: https://tsch.js.org/529

...

type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer R}` ? R : `${T}`

Solution by veralex #33786

type Absolute<T extends number | string | bigint> = ${T} extends -${infer P} ? P :T

Solution by rookiewxy #33744

type Absolute<T extends number | string | bigint> = T extends `${infer First}${infer Rest}` ? (First extends '-' ? Rest : T) : Absolute<`${T}`>

Solution by Leen27 #32805

type Absolute<T extends number> = `${T}` extends `-${infer N}` ? N : T

Solution by ZhulinskiiDanil #32700

type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer U}` ? U : `${T}`
  1. 탬플릿 리터럴 타입에 대해서도 문자열화 시키는 기능으로서 사용할 수 있다.

Solution by dev-hobin #32405

// 解答をここに記入
type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer R}` ? `${R}`: `${T}`

Solution by pea-sys #32177

type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer U}` ? `${U}` : `${T}`;

Solution by dddieon #31906

type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer U}` ? U : `${T}`

Solution by vipulpathak113 #31878

type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer R}` ? R : `${T}`

Solution by jinyoung234 #31806

type Absolute<T extends number | string, K extends string = `${T}`> = K extends `-${infer A}` ? A : K;

Solution by ricky-fn #31297

type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer A}` ? A : `${T}`;

Solution by eward957 #31247

type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer U}` ? U : `${T}`

Solution by MyeonghoonNam #30968

type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer K}` ? K : `${T}`;

Solution by jaeilnet #30544

type Absolute<T extends number | string | bigint> = 
  `${T}` extends `-${infer N}`
    ? N
    : `${T}` extends `${infer N}`
      ? N
      : never;

Solution by wouldRewind #30393

type Absolute<T extends number | string | bigint> = ${T} extends ${infer First}${infer Rest} ? First extends '-' ? ${Rest} : ${T} : never

Solution by agus-wesly #30282

type Test = -100;

type Absolute<T extends number> = `${T}` extends `${infer o}${infer R}` ? R: never;
type Result = Absolute<Test> // expected to be "100"

Solution by rxMATTEO #30097

type Absolute<T extends number | string | bigint> = `${T}` extends`-${infer U}` ? `${U}` : `${T}`

Solution by maximallain #29598


type Absolute<T> = T extends string ?
    T extends `${'-'}${infer R}` ? R : T:
        T extends number | bigint ?
            `${T}` extends `${'-'}${infer R}` ? R : `${T}` : never;

type AbsRes = Absolute<-100>;

Solution by sundial-dreams #29453

type Absolute<T extends number | string | bigint> = 
  `${T}` extends `-${infer R}` 
    ? `${R}` : 
    `${T}`

Solution by MohammadArasteh #29417

// 여기 풀이를 입력하세요
type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer U}` ? U : `${T}`;

Solution by eodnjs467 #29362

type AbsStr<T extends string> = T extends `-${ infer V }` ? V : T
type Absolute<T extends number | string | bigint> = T extends number | bigint ? AbsStr<`${ T }`> : AbsStr<T & string>

Solution by Yirujet #29299

type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer R}` ? R : `${T}`;

Solution by RainbowIsPerfect #29029

type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer S}` ? S : `${T}` 

Solution by qianzhong516 #28978