Skip to content

Add type predicate to Object.is #28753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
5 tasks done
KSXGitHub opened this issue Nov 30, 2018 · 2 comments
Open
5 tasks done

Add type predicate to Object.is #28753

KSXGitHub opened this issue Nov 30, 2018 · 2 comments
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@KSXGitHub
Copy link
Contributor

KSXGitHub commented Nov 30, 2018

Search Terms

object.is

type predicate

Suggestion

When a is b, a and b has to same type, so I suggest adding a few generic overloads with type predicates

interface ObjectConstructor {
  is<A, B extends A> (a: A, b: B): a is B
  is<A extends B, B> (a: A, b: B): b is A
  is (a: any, b: any): boolean
}

Checklist

My suggestion meets these guidelines:

  • 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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@weswigham weswigham added Suggestion An idea for TypeScript In Discussion Not yet reached consensus Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Nov 30, 2018
@edemaine
Copy link

I ran into this issue in realizing that Object.is(a, b) doesn't narrow types in the way that a === b does.

It seems that the new const type parameters of TS 5.0 are superior for type narrowing:

is: (<const T, U extends T>(a: T, b: U) => a is U) &
    (<const T, U extends T>(a: U, b: T) => b is U)

See playground example.

For comparison, the original poster's definition doesn't handle the second case properly.

Perhaps TS5 is the right time to add this better type for Object.is?

@edemaine
Copy link

edemaine commented Jan 22, 2023

On further reflection, we realize that these definitions don't properly handle the case that a and b have partially overlapping types.

A reasonable heuristic for now seems to be this:

const is: {
  <B, A extends B> (a: A, b: B): b is A,
  <A, B> (a: A, b: B): a is A & B
} = Object.is as any;
  • If the second argument is more general, narrow it.
  • Otherwise, narrow the first argument in all cases.

Playground example

Until #26916 this seems to be the best we can do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants