microsoft / microsoft/TypeScript

Number literal type sum bug

Abierto
#42,045 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Needs Investigation
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

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-bn0vKYgHMs7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6TlOgkNoo7Cjwfb4123pA5-6zlzpekAASWcR4kwAIRh2Q1BqC3A6LHPg6EMjQxPkgfupRyhjz4L3OMKYsY41Vujfo6t0xIH-tA5ihsv7IVNjNPMoZwxFBIQ8MhNgsYVCKC8bs6d3jp12EUd4i4VbbECJQkMYYbAUIaidXh4ZaH0MYcw1hDDdgFiOIQx4xDBFeWoaI9hKxlZbCyNwxRGVlHazoVIm2hxuYA39kEdGlhrAQFFlA4e7hYFlD4A3RBMN5YtFQeo9BCt8YPw5MEcxsxLERkGCsdxnCsjSK5k7d+BcODzGRi4lB2s0FqwED4vEJNoG1kNjE2w+NDGjCJA7acOI25nD5gYf23swA2ALOkGGWk-CBALLg2xR4yyNILMYOp0D9bFAcW07MUwZhzBsOkFKbJjAtKNL0uBfAtJDIsbYX+4zWQdzqXwLubpHhdMCJMlZowO7-w2d3bZv9dljPcmyZeMRV7hjWYEQ5+yyAfxiHImwy8nnBGqP4uYViP6fILuPWJ89LmshBjELSQsbAF3yVGEGIx37OAEFU8oKIqk2HTvCLOgRMXp32eizO6duFcAxfCIlqASWYuxRS6pVLqV0qzgvBeIxCZv2IG7aAcBEBIGDBwW4dA163CYL7M4-tjBrx7pA7IPSJbwzJNZAAIhAWQHBxDQPsfA+J0DXGYySR4lJBNsEZNab0w24rKzEK4N4dF7jtj7h7CrJVKrRAhL1Vw6MwSaXWpJQ0ZmaQzb4CthUEoES2ZP3tlzJ1qqTEBiRXQc1CgaSOJRAmx4RRcU4qxSw8ZrKgA

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con el TypeScript Playground enlazado y compara los tipos recursivos Piper y Sum, especialmente el acumulador y el retorno Debug comentado. Reproduce el resultado para Piper<["9", "90"]> y para el ejemplo más largo; después, determina por qué el resultado recursivo obtiene un dígito adicional. Se considera terminado cuando los casos de Piper indicados producen "99" y "9999", mientras que el comportamiento existente de Sum sigue siendo correcto.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Error
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.