Created
June 12, 2024 22:27
-
-
Save Fasteroid/c22e48084db0f7f55aac6cd0d7321250 to your computer and use it in GitHub Desktop.
Ever wanted to write horrible type-unsafe code without using πππ’ or ππππππ π ?
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 Fruit = { | |
name: string | |
color: string | |
sweetness: number | |
} | |
type Apple = Fruit & { | |
variety: string | |
color: "red" | "green" | "yellow" | |
} | |
type Banana = Fruit & { | |
length: number | |
color: "yellow" | |
} | |
type AppleAndBanana = [Apple, Banana] | |
function evil(both: Fruit[]) { | |
const temp = both[0]; | |
both[0] = both[1]; | |
both[1] = temp; | |
} | |
const apple: Apple = { | |
name: "apple", | |
color: "red", | |
sweetness: 7, | |
variety: "red delicious" | |
} | |
const banana: Banana = { | |
name: "banana", | |
color: "yellow", | |
sweetness: 5, | |
length: 6 | |
} | |
const both: AppleAndBanana = [apple, banana]; | |
evil(both); // technically no rules broken | |
console.log(both[1].length); // undefined even though typescript says otherwise | |
console.log(both[0].variety); // ditto |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment