Skip to content

Type instantiation is excessively deep not reported with a complex type involving a conditional type and another aliased conditional typeΒ #50017

Closed as not planned
@Andarist

Description

@Andarist

Bug Report

πŸ”Ž Search Terms

NonNullable, conditional type, excessively deep

πŸ•— Version & Regression Information

  • This changed between versions 4.2 and 4.3 and I've bisected it to this PR

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type StringKeys<O> = Extract<keyof O, string>;
type Values<O> = O[keyof O];

type If<Condition, Then, Else> = Condition extends true ? Then : Else;
type IsObject<T> = T extends Record<string, any> ? true : false;
type IsArray<T> = T extends Array<any> ? true : false;

interface XataRecord {
  id: string;
}

type MAX_RECURSION = 5;

type NestedColumns<O, RecursivePath extends any[]> =
  // this check is important too
  RecursivePath["length"] extends MAX_RECURSION
    ? never
    : Values<
        {
          [K in DataProps<O>]: If<
            IsArray<O[K]>,
            K,
            // this NonNullable is important
            NonNullable<O[K]> extends XataRecord
              ? // NestedColumns (recursive) here seems to be important
                NestedColumns<
                  // this NonNullable is important
                  NonNullable<O[K]>,
                  // both of those are important
                  [...RecursivePath, O[K]]
                > extends string
                ? `${NestedColumns<
                    NonNullable<O[K]>,
                    // both of those are important
                    [...RecursivePath, O[K]]
                  >}`
                : never
              : never
          >;
        }
      >;

type DataProps<O> = Exclude<keyof O & string, keyof XataRecord>;
// it errors with this variant
// type DataProps<O> = keyof O & string;

πŸ™ Actual behavior

It doesn't error on the template literal type.

πŸ™‚ Expected behavior

It should error on the template literal type.

I believe that it should error because:

  1. it was an error in 4.2
  2. a bigger repro of this started to error in nightly versions since the NonNullable change (from a conditional type to T & {}). The slimmed down version that I've created doesn't error in the nightly build though. Original repro by @SferaDev can be found here: 4.7 with no error, 4.8 nightly with the error
  3. If we inline the If<IsArray<O[K]>, ..., ...> alias then it starts to error in all of the versions of the slimmed down repro: TS playground. To the best of my knowledge the aliased version and inlined version should not differ in their observable behaviors.

I understand that the repro is not small - I've tried to reduce it further but I couldn't. The problem at hand seems to be quite complex and involves a very specific ingredients from what I can tell.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions