Skip to content

Applying a constrained generic with type parameters as the argument type doesn't properly catch constrained fields #52309

Closed
@shapirone

Description

@shapirone

Bug Report

🔎 Search Terms

generic constraints, generic types, multiple generics, generic type variable, type parameters

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics.

⏯ Playground Link

Playground link with relevant code

💻 Code

type PropertiesType = {[key:string]: any};
type RequiredPropertiesType<Props extends PropertiesType> = (Exclude<keyof Props, symbol>)[];

interface JSONSchemaObjectType<
  Props extends PropertiesType,
  RequiredProps extends RequiredPropertiesType<Props>
> {
  properties: Props;
  required?: RequiredProps;
  additionalProperties?: boolean;
}

const DefineObject = <
  Props extends PropertiesType,
  RequiredProps extends RequiredPropertiesType<Props>,
  Def extends JSONSchemaObjectType<Props, RequiredProps>
>(definition: Def) => {
  // noop
}

DefineObject({
  properties: {
    test: "hi"
  },
  required: ["test", "broken like me"] // <-- no static type error
})

🙁 Actual behavior

The required array accepts values that are not keys used in it's sibling properties

🙂 Expected behavior

The required array is constrained to the keys used in it's sibling properties

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions