05140-medium-trunc

Back

// your answers
type Trunc<T extends number | string> = `${T}` extends `${infer F extends
  | number}.${infer _}`
  ? `${F}`
  : T extends `${infer F extends "" | "-" | "+"}.${infer _}`
  ? `${F}0`
  : `${T}`;

Solution by shx123qwe #35133

// your answers
type Trunc<T extends number | string> = `${T}` extends `${infer F extends
  | number}.${infer _}`
  ? `${F}`
  : T extends `${infer F extends "" | "-" | "+"}.${infer _}`
  ? `${F}0`
  : `${T}`;

Solution by shx123qwe #35132

type SpecialChar = '-'
type NumberToString<T extends number> = `${T}`;
type Trunc<T, A extends any[] = []> = T extends `${infer F}${infer Rest}` ?
  F extends SpecialChar ? Trunc<Rest, A> : F extends '.' ? A : Trunc<Rest, [...A, F]> : A;
type Array2Str<T extends any[], S extends string = ''> = T extends [infer F, ...infer Rest] ? F extends string ? Array2Str<Rest, `${S}${F}`> : Array2Str<Rest, S> : S


type Trunc<T, A extends string = ''> = (T extends number ? `${T}` : T extends string ? T : never) extends `${infer F}${infer Rest}` ?
  F extends '.' ? A extends '' | '-' ? A extends '-' ? '-0' : '0' : A : Trunc<Rest, `${A}${F}`> : A;
  
type Trunc<T extends number | string> = `${T}` extends `${infer N}.${any}` ? N : `${T}`

Solution by wendao-liu #35074

type Trunc<T extends number | string> = `${T}` extends `${infer R}.${any}` ? (R extends '' | '-' ? `${R}0` : `${R}`) : `${T}`

Solution by ouzexi #34091

type Trunc = ${T} extends ${infer S}.${infer E} ? S : T

Solution by rookiewxy #33877

type FillZero<T extends string> = T extends `.${any}` ? `0${T}` : T;

type Split<T extends string> = T extends `-${infer Rest}`
  ? ["-", FillZero<Rest>]
  : ["", FillZero<T>];

type Trunc<T extends string | number> = Split<`${T}`>[1] extends `${infer Head}.${any}` ? `${Split<`${T}`>[0]}${Head}` : `${T}`

Solution by sunupupup #33414

Hahaha, look what I created!

type Trunc<T extends string | number, C extends string = ''> = `${T}` extends `${infer L}${infer R}`
  ? L extends '.'
    ? C extends ''
      ? '0'
      : C
    : R extends '' ? `${C}${L}` : Trunc<R, `${C}${L}`>
  : T

Solution by tototi5997 #33242

// your answers
type Trunc<T extends string | number> = `${T}` extends `${infer U}.${infer _}` ? (U extends "" ? "0" : U) : `${T}`;

Solution by pea-sys #33100

type GetIntegerFromString<T extends string> = T extends ""
  ? '0'
  : T

type Trunc<N extends string | number> = `${N}` extends `${infer I}.${number}`
  ? GetIntegerFromString<I>
  : `${N}`

Solution by keyurparalkar #32468

type Trunc<T extends string | number> = `${T}` extends `${infer L}.${infer _}` ? (L extends "" ? "0" : L) : `${T}`;

Solution by gasmg #32235

type Trunc<U extends number|string, T = `${U}`> = T extends `${infer L}.${infer R}` ? L extends "" ? '0' : L : T

Solution by Zhen-code #32029

type Trunc<T extends number | string> = `${T}` extends `${infer N}.${any}` ? N : `${T}`

Solution by MyeonghoonNam #31581

type Trunc<T extends string | number> = 
  `${T}` extends `${infer S}.${string}`
    ? `${S extends '' ? 0 : S}`
    : `${T}`

Solution by matallui #30810

type Dot = ".";
type Trunc<T extends string | number> =
  `${T}` extends `${infer First}${Dot}${any}`
    ? First extends ""
      ? "0"
      : First
    : `${T}`;

Solution by WanderedToLa #30742

type Trunc<T extends string | number> = 
  `${T}` extends `${infer Num}.${string}` ? 
    Num extends '' ? '0' : Num 
  : `${T}`

Solution by jjswifty #30272

// your answers
// Equal<F, ''> extends true处理'.3'这个case
type Trunc<T extends string | number> = `${T}` extends `${infer F}.${infer R}`
	? Equal<F, ''> extends true
		? '0'
		: F
	: `${T}`

Solution by bebusy007 #30022

// your answers
type Trunc<T> = T extends `${infer First}.${infer Rest}`? First: never

Solution by kerolossamir165 #29862

type Trunc<N extends number | string> = `${N}` extends `${infer T}.${infer _}`
  ? T extends ""
    ? "0"
    : T
  : `${N}`;

Solution by idebbarh #29853

type ToNumber<T extends string, R extends any[] = []> = `${R['length']}` extends T ?
    R['length']: ToNumber<T, [...R, 0]>;    

type Trunc<T extends number> = `${T}` extends `${infer A}.${infer B}` ?
    ToNumber<A>: T;

type trunc = Trunc<123.456>;

const tr: trunc = 123;

Solution by sundial-dreams #29547

type Trunc<T extends string | number> = T extends number 
  ? Trunc<`${T}`> 
    : T extends `${infer S}.${string}` 
    ? S extends "" 
      ? "0" 
      : S
  : T

Solution by dmytro-shumak #29375

type Trunc<T extends number | string> = `${T}` extends `${infer Head}.${string}`
  ? `${Head}`
  : `${T}`

Solution by Kying-star #29040

type Trunc<T extends string | number> =
  `${T}` extends `${infer Int}.${infer Float}`
    ? Int extends ""
      ? "0"
      : Int
    : `${T}`;

Solution by DoubleWoodLin #28778

// your answers
type Fill0<T extends string | number> = `${T}` extends `.${infer R}` ? `0.${R}` : `${T}`
type Trunc<T extends string | number> = Fill0<T> extends `${infer L}.${any}`
  ? L
  : `${T}`

Solution by GreattitJY #28157

type Trunc<T extends number | string> = T extends number
  ? ExtractIntegerPart<`${T}`>
  : T extends string 
    ? ExtractIntegerPart<T>
    : never

type ExtractIntegerPart<T extends string> = T extends string
  ? T extends `${infer F}.${infer R}`
    ? F extends ''
      ? '0'
      : F
    : T
  : never

Solution by jazelly #27892

type Trunc<T extends string | number> = `${T}` extends `${infer Int}.${any}` ? Int extends '' ? '0' : Int : `${T}`

Solution by jiechliu #27727

// your answers
type Trunc<T extends number> = `${T}` extends `${infer S extends number}.${any}` ? S : T;

Solution by Hencky #27637

type HandleEmptyStr<Res> = Res extends "" ? "0" : Res;

type Trunc<N extends number | string> = HandleEmptyStr<
  `${N}` extends `${infer First}.${string}` ? First : `${N}`
>;

Solution by alythobani #27609

type Trunc<T extends number | string> = `${T}` extends `${infer R}.${infer _}`
  ? R
  : `${T}`;

Solution by ryuji-1to #27367

type Trunc<T extends string | number> = `${T}` extends `${infer L}.${infer _}` ? L extends '' ? '0' : L : `${T}`

Solution by smileboyi #27106

// your answers
type Trunc<T extends number | string> = 
  `${T}` extends `.${infer X}` 
    ? '0' 
    : `${T}` extends `${infer H}.${infer R}`
      ? H 
      : `${T}`

Solution by kiki-zjq #25668