Last active
April 19, 2019 19:06
-
-
Save TheCubicleBuddha/c03a1ac37550f8a34df08a3b03915f40 to your computer and use it in GitHub Desktop.
This demonstrates how you can use TypeScript to clearly communicate your needs upfront
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
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 | |
// // this will never happen | |
// } | |
return dbInstance.save(newUserToSave); | |
} | |
honestDbMethod({ firstName: "Madonna" }, UserDB); | |
// ^ Compliler Error: Property 'lastName' is missing in type '{ firstName: string; }' but required in type 'INewUser'. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I left the UserDatabase.ts file for brevity, but here it is: