Created
March 26, 2019 00:41
-
-
Save micahriggan/4cc7b71886b26013d87736aa9c248628 to your computer and use it in GitHub Desktop.
Showing the errors that can occur from using any instead of a generic
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
interface Person { | |
name: string; | |
age: number; | |
} | |
interface Car { | |
modelName: string; | |
year: number; | |
} | |
class BadTable { | |
rows = new Array<any>() | |
addRow(row: any) { | |
this.rows.push(row); | |
} | |
getColumn(col: keyof any) { | |
return this.rows.map(r => r[col]); | |
} | |
} | |
function testBadTable() { | |
const people = new BadTable() | |
people.addRow({name: 'Micah'}); | |
people.addRow({name: 'Micah', age: 25}); | |
const cars = new BadTable(); | |
cars.addRow({modelName: 'Toyota Something or Other', year: 2010}); | |
cars.addRow({modelNme: 'Honda Something or Other', year: 2010}); | |
cars.getColumn("modelName"); | |
cars.getColumn("year"); | |
cars.getColumn("age"); // [undefined, undefined] | |
} | |
/* | |
* LOGS | |
* [ 'Micah' ] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment