Skip to content

Generic Extended Types not inferring Correctly #48741

Closed
@jaredjbarnes

Description

@jaredjbarnes

Bug Report

🔎 Search Terms

  • Generic Extended Types not inferring Correctly
  • Inferred generics

🕗 Version & Regression Information

All versions have this problem.

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "Inferred types & Generics"

⏯ Playground Link

Playground link with relevant code

💻 Code

type UnWrapValue<T> = T extends Array<infer TValue> ? TValue | null : never;

type UnWrapObject<T> = {
  [P in keyof T]: UnWrapValue<T[P]>;
};

interface DefaultProps {
  prop1: number[];
  prop2: string[];
}

interface ExtendedProps extends DefaultProps {
  prop3: boolean[];
}

class SuperKlass<T> {
  unwrapped!: UnWrapObject<T>;
  original!: T;
}

// It appears to me that the default generic type isn't being accounted for here.
// I would expect T to be at least DefaultProps and and be able to get that typing, however, it 
// is unable to infer the typing.
class SubKlass<T extends DefaultProps = DefaultProps> extends SuperKlass<T> {
  method1(){
    // Why does the typing NOT work here?
    this.unwrapped.prop1 = 1;
  }
}

class OneDeeper extends SubKlass<ExtendedProps> {
  method2(){
    // Why does the typing work here?
    this.unwrapped.prop1 = 1;
    this.unwrapped.prop3 = false;
  }
}

🙁 Actual behavior

Unwrapping a constrained generic type with "infer" fails to do so properly.

🙂 Expected behavior

"this.unwrapped.prop1" should be type "string" within "SubKlass".

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions