microsoft / microsoft/TypeScript

Number literal type sum bug

Open
#42,045 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 4.12

Search Terms:
Sum, literal, not working correct

Code

namespace Shared {
  export type Num = string | number | bigint;

  export type Preformat<T extends Shared.Num, U extends string = `${T}`, Acc extends string[] = []> = U extends `${infer Head}${infer Rest}`
    ? Preformat<T, Rest, [Head, ...Acc]>
    : Acc

  export type Helper<T, U0, U1, U2, U3, U4, U5, U6, U7, U8, U9> = 
    T extends '0' ? U0 : T extends '1' ? U1 : T extends '2'
                  ? U2 : T extends '3' ? U3 : T extends '4'
                  ? U4 : T extends '5' ? U5 : T extends '6'
                  ? U6 : T extends '7' ? U7 : T extends '8'
                  ? U8 : T extends '9' ? U9 : never

  export type HelperMap<R> = R extends [infer T, infer U]
    ? Shared.Helper<
        T,
        Shared.Helper<U, ['0', false], ['1', false], ['2', false], ['3', false], ['4', false], ['5', false], ['6', false], ['7', false], ['8', false], ['9', false]>,
        Shared.Helper<U, ['1', false], ['2', false], ['3', false], ['4', false], ['5', false], ['6', false], ['7', false], ['8', false], ['9', false], ['0', true]>,
        Shared.Helper<U, ['2', false], ['3', false], ['4', false], ['5', false], ['6', false], ['7', false], ['8', false], ['9', false], ['0', true], ['1', true]>,
        Shared.Helper<U, ['3', false], ['4', false], ['5', false], ['6', false], ['7', false], ['8', false], ['9', false], ['0', true], ['1', true], ['2', true]>,
        Shared.Helper<U, ['4', false], ['5', false], ['6', false], ['7', false], ['8', false], ['9', false], ['0', true], ['1', true], ['2', true], ['3', true]>,
        Shared.Helper<U, ['5', false], ['6', false], ['7', false], ['8', false], ['9', false], ['0', true], ['1', true], ['2', true], ['3', true], ['4', true]>,
        Shared.Helper<U, ['6', false], ['7', false], ['8', false], ['9', false], ['0', true], ['1', true], ['2', true], ['3', true], ['4', true], ['5', true]>,
        Shared.Helper<U, ['7', false], ['8', false], ['9', false], ['0', true], ['1', true], ['2', true], ['3', true], ['4', true], ['5', true], ['6', true]>,
        Shared.Helper<U, ['8', false], ['9', false], ['0', true], ['1', true], ['2', true], ['3', true], ['4', true], ['5', true], ['6', true], ['7', true]>,
        Shared.Helper<U, ['9', false], ['0', true], ['1', true], ['2', true], ['3', true], ['4', true], ['5', true], ['6', true], ['7', true], ['8', true]>
      >
    : never

  export type Join<T, Acc extends string = ''> = T extends [infer Head, ...infer Rest]
    ? Head extends string 
      ? Join<Rest, `${Acc}${Head}`>
      : never
    : Acc

  export type JoinR<T, Acc extends string = ''> = T extends [infer Head, ...infer Rest]
    ? Head extends string 
      ? JoinR<Rest, `${Head}${Acc}`>
      : never
    : Acc
}

namespace Sum {
  type Nulling<T> = {
    [K in keyof T]: "0"
  }

  type Concat<T extends any[], U extends any[]> = [...T, ...U]

  type Liner<T extends any[], U extends any[]> = Concat<T, Nulling<U>>

  type Zip<T, U> = {
    [K in keyof T]: K extends keyof U
      ? [T[K], U[K]]
      : never
  }

  type Iter<T, B extends boolean = false, Acc extends any[] = []> = T extends [infer Head, ...infer Rest]
    ? B extends true
      ? Iter<Rest, Shared.HelperMap<[Shared.HelperMap<Head>[0], "1"]>[1], [...Acc, Shared.HelperMap<[Shared.HelperMap<Head>[0], "1"]>[0]]>
      : Iter<Rest, Shared.HelperMap<Head>[1], [ ...Acc, Shared.HelperMap<Head>[0]]>
    : Acc

  type Unformate<T, Acc extends any[] = []> = T extends [infer Head, ...infer Rest]
    ? Unformate<Rest, [Head, ...Acc]>
    : Acc

  type Cut<T> = T extends [infer Head, ...infer Rest]
    ? Head extends '0'
      ? Cut<Rest>
      : T
    : ['0']

  export type Sum<
      A extends Shared.Num, 
      B extends Shared.Num,
      PA extends string[] = Shared.Preformat<A>,
      PB extends string[] = Shared.Preformat<B>,
      LA = Liner<PA, PB>,
      LB = Liner<PB, PA>,
      Z = Zip<LA, LB>,
      I = Iter<Z>,
      U = Unformate<I>,
      C = Cut<U>,
      J = Shared.Join<C>
    > = J
  
  type testSum = [
    Sum<"90", "9">,
    Sum<"0", Sum<"9", Sum<"90", Sum<"900", "9000">>>>
  ]
}

namespace Multiply {
  type Piper<T, Acc extends string = '0', Debug extends any[] = []> = T extends [infer Head, ...infer Rest]
    ? Head extends string
      ? Piper<Rest, Sum.Sum<Head, Acc>, [...Debug, [Head, Acc, Rest, Sum.Sum<Head, Acc>]]>
      : never
    : Acc

  type testPiper1 = [
    Piper<["9", "90"]>,
  ]
}

Expected behavior:

Looks first on two main types: Sum.Sum and Piper. When i use Piper with ["9", "90"] its return strange result like "999", but must "99". I tested my Sum.Sum type and it work correct

Then look on Piper Debug type. Looks like on first iteration they have correct Head and Rest types, but result incorrect

  type Piper<T, Acc extends string = '0', Debug extends any[] = []> = T extends [infer Head, ...infer Rest]
    ? Head extends string
      ? Piper<Rest, Sum.Sum<Head, Acc>, [...Debug, [Head, Acc, Rest, Sum.Sum<Head, Acc>]]>
      : never
+    : Debug

Actual behavior:
Piper<["9", "90", "900", "9000"]> must return correct value "9999" like Sum<"0", Sum<"9", Sum<"90", Sum<"900", "9000">>>>

Playground Link: https://www.typescriptlang.org/play?#code/HYQwtgpgzgDiDGEAEBlAFiAThAJkg3gLABQSSEAHjAPaYAuSdAnjMgHICuYSAvElHUwBLYAHMkAHyTAuAIwiZJSWUNEi6AbhIkylGvUYtkABWwAzWmBB0APABVyFOhGA4oqDNhwA6TmAA0SACqjs6u7gLCYrxIAAYAJPh2AL6xgQCC8PChLm78giKiANoAujGlAHwxIZRheQn4ImYKSAASECA4yYlNLQBK0HSpOmRIAPxIphAWmFa2doEDAoFF7Z2B3puZ8CUVI2QAXEjb2qSO+gzMrG0QADasmPaBQQAMzwCMzwBMzwDMzwAWZ4AVmeADZngB2Z4ADmeAE4qnx9kgHLVcu4AOQvTHjYIvJBHNFODFITHvXETILvQmonLhMlfTEo0astlkKlfWnEupY36U4K-bn0vKYgHMs7sqWjKkA4XohmY4ECoLA+Ukx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6zlzpekAASWcR4kwAIRh2Q1BqC3A6LHPg6EMjQxPkgfupRyhjz4L3OMKYsY41Vujfo6t0xIH-tA5ihsv7IVNjNPMoZwxFBIQ8MhNgsYVCKC8bs6d3jp12EUd4i4VbbECJQkMYYbAUIaidXh4ZaH0MYcw1hDDdgFiOIQx4xDBFeWoaI9hKxlZbCyNwxRGVlHazoVIm2hxuYA39kEdGlhrAQFFlA4e7hYFlD4A3RBMN5YtFQeo9BCt8YPw5MEcxsxLERkGCsdxnCsjSK5k7d+BcODzGRi4lB2s0FqwED4vEJNoG1kNjE2w+NDGjCJA7acOI25nD5gYf23swA2ALOkGGWk-CBALLg2xR4yyNILMYOp0D9bFAcW07MUwZhzBsOkFKbJjAtKNL0uBfAtJDIsbYX+4zWQdzqXwLubpHhdMCJMlZowO7-w2d3bZv9dljPcmyZeMRV7hjWYEQ5+yyAfxiHImwy8nnBGqP4uYViP6fILuPWJ89LmshBjELSQsbAF3yVGEGIx37OAEFU8oKIqk2HTvCLOgRMXp32eizO6duFcAxfCIlqASWYuxRS6pVLqV0qzgvBeIxCZv2IG7aAcBEBIGDBwW4dA163CYL7M4-tjBrx7pA7IPSJbwzJNZAAIhAWQHBxDQPsfA+J0DXGYySR4lJBNsEZNab0w24rKzEK4N4dF7jtj7h7CrJVKrRAhL1Vw6MwSaXWpJQ0ZmaQzb4CthUEoES2ZP3tlzJ1qqTEBiRXQc1CgaSOJRAmx4RRcU4qxSw8ZrKgA

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground and compare the recursive Piper and Sum types, especially the accumulator and the commented Debug return. Reproduce the result for Piper<["9", "90"]> and the longer example, then determine why the recursive result gains an extra digit. Done means the reported Piper cases produce "99" and "9999" while the existing Sum behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.