microsoft / microsoft/TypeScript

`satisfies` operator could not completely overshadow the existing contextual type

Open
#57,667 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Proposal Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the motivating example and compare the ordinary call with the version using satisfies. Read the related discussion in issue 57431 and trace how contextual typing and satisfies interact. Done means the extra constraint is retained without causing implicit-any errors in the callback parameters, while the existing checks remain intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.