View dontBeFlexibleIfYouHaveAnOpinion.ts
function getUserById(id: any){ | |
// hits a database and returns the value | |
} | |
const guid = "b5f770f2-197c-4bf5-a31f-cfec4f545d06"; | |
const user = getUserById(guid); | |
// ^^^ Runtime Error: PL/SQL: ORA-00932: inconsistent datatypes: expected NUMBER got - TEXT |
View dontBeFlexibleIfYouHaveAnOpinion.ts
function getUserById(id: any){ | |
// hits a database and returns the value | |
} | |
const guid = "b5f770f2-197c-4bf5-a31f-cfec4f545d06"; | |
const user = getUserById(guid); | |
// ^^^ Runtime Error: PL/SQL: ORA-00932: inconsistent datatypes: expected NUMBER got - TEXT |
View lettingTheDevKnowUpFront.ts
function getUserById(id: number){ | |
// hits a database and returns the value | |
} | |
const guid = "b5f770f2-197c-4bf5-a31f-cfec4f545d06"; | |
const user = getUserById(guid); | |
// ^ Compiler error: Argument of type string is not assignable to parameter of type 'number'. |
View passiveAggressiveUserSaver.js
function martyrAlgorithm(newUserToSave){ | |
if(!newUserToSave.lastName){ | |
throw new Error("I did my best, but I can't save this new user since you didn't get me a lastName") | |
} | |
globals.databaseHandle.save(newUserToSave); | |
} |
View forthrightUserSaver.ts
import { IUserDbInstance, UserDB } from 'UserDatabase'; | |
interface INewUser { | |
firstName: string, | |
middleName?: string, | |
lastName: string | |
} | |
function honestDbMethod(newUserToSave: INewUser, dbInstance: IUserDbInstance){ | |
// if(newUserToSave.lastName){ // This if statement is unnecessary because you asked for what you need |
View theFutureIntroducedABugInTrafficSignals.ts
type ITrafficLight = "red" | |
| "redBlinking" // <-- new case | |
| "green" | |
| "yellow" | |
| "yellowBlinking"; // <-- new case | |
// Can you spot the bug? | |
// You can skip ahead to the answer here: https://gist.github.com/TheCubicleBuddha/7a8854f244697b9894e41d44e1fc6967 | |
function respondToTrafficSignal(signal: ITrafficLight): "stop" | "go" | "pause" { |
View simpleTrafficSignals.ts
type ITrafficLight = "red" | "green" | "yellow"; | |
// Can you see where the function below might go wrong in the future? | |
// You can skip ahead to the answer here: https://gist.github.com/TheCubicleBuddha/7a8854f244697b9894e41d44e1fc6967 | |
function respondToTrafficSignal(signal: ITrafficLight): "stop" | "go" | "pause" { | |
if(signal === "red"){ | |
return "stop"; | |
} else if(signal === "yellow"){ | |
return "pause"; |
View exhaustiveTrafficSignals.ts
type ITrafficLight = "red" | "redBlinking" | "green" | "yellow" | "yellowBlinking"; | |
function respondToTrafficSignal(signal: ITrafficLight): "stop" | "go" | "pause" { | |
if(signal === "red"){ | |
return "stop"; | |
} else if(signal === "yellow"){ | |
return "pause"; | |
} else if(signal === "green") { | |
return "go"; | |
} else { |