Skip to content
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

Add isEqualType method? #2

Open
sindresorhus opened this issue Oct 16, 2021 · 9 comments
Open

Add isEqualType method? #2

sindresorhus opened this issue Oct 16, 2021 · 9 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@sindresorhus
Copy link
Owner

To ensure a type is correct. Can be useful for testing.

We could use this type for it: https://github.com/sindresorhus/type-fest/blob/98158e0fcb354e36c8aaf4b6808ca1498156f1f4/source/internal.d.ts#L1-L11

Example:

// Type
isEqualType<ExpectedType, ActualType>();

// Value
isEqualType(expectedValue, actualValue);

// Mixed
isEqualType<ExpectedType>()(value);

Thoughts?

@bradgarropy
Copy link

I've wanted this for a long time!

@sindresorhus sindresorhus added enhancement New feature or request help wanted Extra attention is needed labels Oct 18, 2021
@ritikbheda
Copy link

Hello there @sindresorhus and @bradgarropy, I would try to work on this enhancement. I am clear about Type and Value but I really did not understand the Mixed functions you mentioned above. If you could please give more information on that function, that would be great help!

@sindresorhus
Copy link
Owner Author

@ritikbheda The mixed one is comparing the generic type against the value, which means you just need to get the type of the input value before comparing.

@younho9
Copy link
Contributor

younho9 commented Jan 5, 2022

Could Value type comparison use deep object comparison?

@sindresorhus
Copy link
Owner Author

@younho9 I don't see why it would. It's about comparing type, not structure.

@younho9
Copy link
Contributor

younho9 commented Jan 5, 2022

@younho9 I don't see why it would. It's about comparing type, not structure.

Sorry. Could object structure comparison be performed deeply at runtime?

For Value type comparison, could it be used as a type guard in runtime? (just a question.)

declare const a: number;
declare const b: unknown;

if (isEqualType(a, b) {
	b
	// => number
}

@sindresorhus
Copy link
Owner Author

Could object structure comparison be performed deeply at runtime?

That's not the goal of this type.

@sindresorhus
Copy link
Owner Author

For Value type comparison, could it be used as a type guard in runtime? (just a question.)

The goal of this is to work with known types at compile time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants