Last active
August 19, 2019 22:14
-
-
Save jbmilgrom/6c4f11c1e122c4a29c4407e8121cc44c to your computer and use it in GitHub Desktop.
An example of "sameness" in the context of immutability (> TS 3.4 needed @see https://devblogs.microsoft.com/typescript/announcing-typescript-3-4/#improvements-for-readonlyarray-and-readonly-tuples)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type NumberPair = readonly [number, number]; | |
const areNumbersEqual = (a: number, b: number) => a === b; | |
const areNumberPairsEqual = (a: NumberPair, b: NumberPair) => | |
areNumbersEqual(a[0], b[0]) && areNumbersEqual(a[1], b[1]); | |
const p1 = [1, 2] as const; | |
const p2 = [1, 2] as const; | |
const p3 = [1, 3] as const; | |
areNumberPairsEqual(p1, p2); // true | |
areNumberPairsEqual(p1, p3); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment