Type Challenges Judge

MinusOne

提出詳細

type MinusOneDigit = { "9": "8", "8": "7", "7": "6", "6": "5", "5": "4", "4": "3", "3": "2", "2": "1", "1": "0", "0": "9" } type Zero = "0" type IsZeros<T extends string> = T extends `${infer D}${infer Tail}` ? D extends Zero ? IsZeros<Tail> : false : true type ToNines<T extends string> = T extends `${string}${infer Tail}` ? `9${ToNines<Tail>}` : "" type MinusOneImpl<T extends string> = T extends `${infer Head extends keyof MinusOneDigit}${infer Tail}` ? IsZeros<Tail> extends true ? `${MinusOneDigit[Head]}${ToNines<Tail>}` : `${Head}${MinusOneImpl<Tail>}` : never type RemoveLeadingZeros<S extends string> = S extends `0${infer Tail}` ? Tail extends "" ? "0" : Tail : S type ParseInt<S extends string> = RemoveLeadingZeros<S> extends `${infer N extends number}` ? N : never type MinusOne<N extends number> = N extends 0 ? -1 : ParseInt<MinusOneImpl<`${N}`>>
提出日時2023-09-15 07:24:07
問題MinusOne
ユーザーsankantsu
ステータスAccepted
テストケース
import type { Equal, Expect } from '@type-challenges/utils' type cases = [ Expect<Equal<MinusOne<1>, 0>>, Expect<Equal<MinusOne<55>, 54>>, Expect<Equal<MinusOne<3>, 2>>, Expect<Equal<MinusOne<100>, 99>>, Expect<Equal<MinusOne<1101>, 1100>>, ]