sindresorhus / ts-extras

Essential utilities for TypeScript projects
MIT License
587 stars 15 forks source link

Add `isEqualType` method? #2

Open sindresorhus opened 3 years ago

sindresorhus commented 3 years ago

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 commented 3 years ago

I've wanted this for a long time!

ritikbheda commented 3 years ago

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 commented 3 years ago

@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 commented 2 years ago

Could Value type comparison use deep object comparison?

sindresorhus commented 2 years ago

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

younho9 commented 2 years ago

@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 commented 2 years ago

Could object structure comparison be performed deeply at runtime?

That's not the goal of this type.

sindresorhus commented 2 years ago

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.

tychenjiajun commented 2 years ago

@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
}

A naive approach https://www.typescriptlang.org/play?#code/LAKAZgrgdgxgLgSwPZQAQIM4FECOECGANgDwCCANKgEKoCmAHnLVACYaqkB8AFEnABa0ATgC5qASlQBvUKjmpIsRCjo5uANyIRaY0uLGbC29OxoyQ8y+jCoNW2qgC8z1H0FDJQ2nAhC0cIW0AblkrOQQbOyMHFygIQkJUAB8k1wFhJ0dUOITk1O44AE8AB1okG0NjAEIXAHIkACMAK1p4WtQAMg7UItLytPdUGqz65ta4WvFJczCwrx8-VEqHYaX7Tu63DNWtoRCLWYBfULCI21IhIXxCgDpMC6vCqO1JLo5L67uMB+vedI9pidZuFIssboRmABzARDFy7cFQgSeby+NBgIgYWj7YGWeaotbRG60dTCJ4aSgISSOTgmXAEQgacR-dwAbQQAF0ptijkDLGcCiUyhV1s4sgAiRoteBijY9QX9XaZEaS8aTaS82b8gDyY3gN0h3gACkI+HxBVqwM9aJJVjqpXB9UaTXAzaULczhFN1QccVY8Yt0YRMdzfccfTj+VVMHSiNw7eMbgBrWiFDBWqZx3UO5Opj0A5ELNEYrEasIAejLqAAAnAMABaBCQqBILylv0oxbxvXMAIIWhp5biIkkoRklmJyjqTlOGnRvCx9RM3bjzniNth4H+otBkvhjfyLeqfZh0AwFAYOA9fuXrIsgCMlAATJRanfauz9qfz5emBfH04rwvG4wAQQgmCEbg53pbg7y5UAvygC9ALgABmACWSkBQkCQMQ71QQ5KEwhp8FEVB-0OD94JAM9EJ-a8ABYAN-VDgNA8DIOwecGUwsBsNw-C4JABCkOYgBWdCeL46QsJw1A8MOfDCNQYjSMk2T5PwyihOo79kIANiY69RNYsDhA4mNuJksQ1P4hTDkEqiaJE68AHZ0PvJ92UoFkUMoeivNQFlX1qF9H3fLThLoi8AA5DIvFyTPYqDYw8sjV0-HTEKQCFwSQSECmvZ9kPoyhmL00rr2i8QgiAA