Skip to content

Instantly share code, notes, and snippets.

@micahriggan
Created March 26, 2019 00:41
Show Gist options
  • Save micahriggan/4cc7b71886b26013d87736aa9c248628 to your computer and use it in GitHub Desktop.
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
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