Created
March 20, 2019 00:47
-
-
Save bsansouci/5e49178af4cb8036e84f1ba310c022c1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Bug 1 | |
// Typescript only looks at the fields and methods to type check, not the constructors. | |
// Root cause: structural typing | |
class Animal { | |
feet: number; | |
constructor(name: string, numFeet: number) { } | |
} | |
class Size { | |
feet: number; | |
constructor(numFeet2: number) { } | |
} | |
let a: Animal = new Size(1); // You probably didn't mean to do that. | |
// Bug 2 | |
// Typescript does not help me refactor this code | |
// Root cause: implicit type coercion in if condition | |
function doSomething(): boolean { | |
return false; | |
} | |
if (doSomething()) { // all good! | |
console.log(doSomething()); | |
} | |
function doSomethingWrong(): Promise<boolean> { | |
return new Promise((resolve) => resolve(false)); | |
} | |
if (doSomethingWrong()) { // Oh no, bug! This is always true | |
console.log(doSomethingWrong()); | |
} | |
// Bug 3 | |
// Typescript does no help me refactor this code. | |
// Root cause: implicit polymorphism of `+` operator | |
let getA = (): string => "first "; | |
let getB = (): string => "and second"; | |
let doSomethingGeneric = () => getA() + getB(); | |
let c = doSomethingGeneric(); // type string | |
let getB2 = (): number => 2; | |
let doSomethingGeneric2 = () => getA() + getB2(); | |
let c2 = doSomethingGeneric2(); // type string | |
// Bug 4 | |
// Typescript doesn't help me refactor this code. | |
// Root cause: does not enforce that statements return `unit` (nothing) type | |
let doSomethingCool = () => { | |
return 1; | |
} | |
let main = () => { | |
// do stuff ... | |
doSomethingCool(); // everything is fine | |
// do other stuff ... | |
}; | |
let doSomethingCool2 = async () => { | |
return await new Promise((resolve) => resolve(1)); | |
}; | |
let main2 = () => { | |
// some code ... | |
doSomethingCool2(); // Wrong, you should await, but not type error | |
// some code ... | |
}; | |
// Bug 5 | |
// Typescript does not help me refactor this code | |
// Root cause: when running in the browser context, every property in `window` is available | |
let main3 = () => { | |
let name = "Hey"; | |
// some code ... | |
console.log(name); // everything is fine | |
}; | |
let main4 = () => { | |
// some code ... | |
console.log(name); // Wrong because we wanted to remove the declaration of `name`. | |
}; | |
// Bug 6 | |
let x = () => ({name: "Alice"}); | |
let y = () => ({name: "Alice", location: "Seattle"}); | |
x = y; // OK | |
y = x; // Error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TS playground