sindresorhus / is

Type check values
MIT License
1.68k stars 109 forks source link

Proposal: `is.equalType()` #145

Closed younho9 closed 2 years ago

younho9 commented 2 years ago

It would be useful as a type guard function of inferring the type of another variable compared to the type of one variable.

is.equal = <A>(a: A, b: unknown): b is A => is(a) === is(b);

/**
const b: unknown = 'unknown value';

if(is.equal('hello', b)) {
    b // b is string.
}
*/

It would be not strict type level equal.

Related: https://github.com/sindresorhus/ts-extras/issues/2

sindresorhus commented 2 years ago

I'm not sure this is a good idea. It would work fine for simple things, but for most custom objects, the type would just be Object, which means it would be true even when the objects differ.

younho9 commented 2 years ago

Your point is right... To correctly infer the object type, a deep identity comparison is required, but I think it's too expensive for type inference.