Skip to content

Instantly share code, notes, and snippets.

@Fasteroid
Created June 12, 2024 22:27
Show Gist options
  • Save Fasteroid/c22e48084db0f7f55aac6cd0d7321250 to your computer and use it in GitHub Desktop.
Save Fasteroid/c22e48084db0f7f55aac6cd0d7321250 to your computer and use it in GitHub Desktop.
Ever wanted to write horrible type-unsafe code without using πšŠπš—πš’ or πšžπš—πš”πš—πš˜πš πš— ?
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