Skip to content

Const type parameters may resolve into non-const types when iterating over Record<string, () => void> parameter #62802

@sanniassin

Description

@sanniassin

🔎 Search Terms

const type parameters inference deep function return type

🕗 Version & Regression Information

  • This is the behavior in every version I tried from 5.0.4 up to 5.9.3 and nightly builds, and I reviewed the FAQ for entries about type inference and const type parameters

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.3#code/PQKhCgAIUgVALAppALgJwK6IMIHsB2AJgJYrEGQAOAhmipLgGapKQDGBJZB1ANpIgAelNIgDOY8vkhj4uDL0KR8iAG6I0kAEbJR1NkiXUi25NWW4AtLkoAaKDC0Z6xZgHdkAc0T00xJUwsyBxEpFJ8AsKiElIMmqIoGGjSxgCeKPDE+J4MGRosxnAAYhj4bNz4YkGi9tBB0lmMGohlyFp6ANZiDsDgKKmUyACSKBrUowDy6mglZRViADyws+VSVUKjRFUA3pAA2h2IqQBcMuhZngC6pwAUAJSQALwAfJCquP6QAL6vj1DFpVWBHWgk2hCqKmm-0gkAA-AC5mtIMBgJAhvgmqJWtpOlU3KQ5M4WMQQSJxJICNDIKdtlSYTC9gBpSBZSCHVKBZaA+bXBFAypMy6RMFVe5PV6NfKwABKPiS+FgA0QdPpMPhYpecFliWSisGKtVp0hGgA3CqvmbwCExPRqGI8JUUHrEEpHpAFv9rfQZXLdUrauxgd6VvNhS1wZBZRw0IQFja-NlbJANa8fTqFUrnvZnjd-tFcLxpmJTiMxpNpiG1ktK8Cs+AHpr84WNGJLV7A47na7IHaHTauzdaTCAELGDq3BuvQdU-ynADkWjHlgAjHOA187vYN5aUZBR-gOkVpABbDA2yBN9Qs-AoXCQAAGKcguz0hAIvFSLMIpwARIuDyuP4mt8959Eqe5jkeTyoEqgRel2AB0+4dCaQA

💻 Code

/**
 * The trueCondition part of the conditional expression should never be reached and be a no-op,
 * but if we get rid of the conditional expression or return anything other than TFunctions there,
 * then inference breaks
 */
type IterateOverFunctions<TFunctions extends { [key: string]: () => void }> =
  TFunctions extends never
    ? TFunctions // Inference breaks without this expression
    : {
        [K in keyof TFunctions]: TFunctions[K] extends () => infer TReturnType
          ? () => TReturnType
          : never;
      };

const asConstTyped = <
  const TReturnType,
  const TFunctions extends Record<string, () => TReturnType>,
>(
  resolvers: IterateOverFunctions<TFunctions>,
) => resolvers;

const constTyped = asConstTyped({
  Bank: () => ({
    id: 'bank-1',
  }),
});

// BankFn must resolve into `() => { readonly id: "bank-1"; }`
type BankFn = typeof constTyped.Bank;

🙁 Actual behavior

Without the seemingly meaningless conditional expression in IterateOverFunctions type constTyped.Bank resolves into

() => unknown

🙂 Expected behavior

IterateOverFunctions type without the conditional expression should resolve constTyped.Bank into the same narrowed down type as with it

() => {
    readonly id: "bank-1";
}

Additional information about the issue

The workaround in IterateOverFunctions doesn't work consistently and breaks when a more sophisticated iteration is going over the inferred types

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions