Skip to content

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

Open
@Andarist

Description

@Andarist
Contributor

πŸ” Search Terms

satisfies contextual type implicit any parameters

βœ… Viability Checklist

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
    This wouldn't change the runtime behavior of existing JavaScript code
    This could be implemented without emitting different JS based on the types of the expressions
    This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
    This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

⭐ 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

fatcerberus

fatcerberus commented on Mar 6, 2024

@fatcerberus

Doesn't this require a formal mechanism for "merging" arbitrary types? Any expression can be contextually typed by anything...

added
SuggestionAn idea for TypeScript
Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
on Mar 6, 2024
RyanCavanaugh

RyanCavanaugh commented on Mar 6, 2024

@RyanCavanaugh
Member

Doesn't this require a formal mechanism for "merging" arbitrary types

Yep, which is why we didn't do this initially. Given

const x: T = e satisfies U;

we need something that merges T and U.

Intersection was proposed as a mechanism here but was found to be insufficient; see #47920 (comment)

Andarist

Andarist commented on Mar 6, 2024

@Andarist
ContributorAuthor

It's nice to see your comment, I couldn't find any comment that would mention this but your comment is very on point πŸ‘

fatcerberus

fatcerberus commented on Mar 6, 2024

@fatcerberus

Oh crap @Andarist got replaced with an AI 🚎 (it reminds me of those spam comments on blogs that are just awkwardly worded variants of β€œgood job on this article, very informative” etc. with an url at the bottom that’s the actual payload)

Andarist

Andarist commented on Mar 6, 2024

@Andarist
ContributorAuthor

As an AI language model I try to show respect where respect is due

RyanCavanaugh

RyanCavanaugh commented on Mar 6, 2024

@RyanCavanaugh
Member

Frankly a low moment on this repo for neither @fatcerberus nor @Andarist to immediately recall a comment buried 48 comments deep in a "Read More..." on a closed issue from 2 years ago

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

Metadata

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

        Participants

        @fatcerberus@RyanCavanaugh@Andarist

        Issue actions

          `satisfies` operator could not completely overshadow the existing contextual type Β· Issue #57667 Β· microsoft/TypeScript