Skip to content

satisfies operator could not completely overshadow the existing contextual typeΒ #57667

Open
@Andarist

Description

πŸ” Search Terms

satisfies contextual type implicit any parameters

βœ… Viability Checklist

⭐ Suggestion

It would be great if satisfies wouldn't completely disassociate the expression from the existing contextual type.

πŸ“ƒ Motivating Example

declare function fn<T extends Record<string, (arg: number) => void>>(
  obj: T,
): void;

// this works without problems
fn({
  foo: (a) => {},
  bar: (a) => {},
});

// but we can't add extra constraints through `satisfies` without breaking existing contextual  typing
fn({
  foo: (a) => {}, // Parameter 'a' implicitly has an 'any' type.(7006)
  bar: (a) => {}, // Parameter 'a' implicitly has an 'any' type.(7006)
} satisfies Record<"foo" | "bar", unknown>);

πŸ’» Use Cases

  1. What do you want to use this for?

It would be nice, at times, to add some extra constraints in places where the existing contextual type exists

  1. What shortcomings exist with current approaches?

satisfies breaks existing contextual typing so things like contextual parameters "break". It requires us to type the parameters manually or to introduce identity functions with those extra constraints like below

  1. What workarounds are you using in the meantime?

This is one of the possible workarounds:

declare function fn<T extends Record<string, (arg: number) => void>>(
  obj: T,
): void;

declare function identityConstraint<T extends Record<"foo" | "bar", unknown>>(
  arg: T,
): T;

fn(
  identityConstraint({
    foo: (a) => {},
    bar: (a) => {},
  }),
);

However, this has an unintended side-effect: excess property check doesn't apply to this object now.


A similar shortcoming related to ThisType and satisfies combination has been recently reported here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions