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
function parseObject(obj: Record<string, unknown>) { | |
let doesNotExist = obj['doesNotExist']; | |
doesNotExist.toString(); // Error: 'doesNotExist' is of type 'unknown'.ts(18046) | |
doesNotExist += 100; // Error: 'doesNotExist' is of type 'unknown'.ts(18046) | |
} | |
parseObject({ ...hero }); // Works |
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
parseObject( | |
addIndexSignature(() => { console.log('whoops'); }) // This should be an Error | |
); |
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
type IndexSignature<T extends {}> = { | |
[K in keyof T]: T[K]; | |
}; | |
interface Hero { name: string; } | |
const hero: Hero = { name: 'codey' }; | |
function addIndexSignature<T extends {}>(obj: T): IndexSignature<T> { | |
return obj; | |
} |
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
function parseObject(obj: Record<string, any>) { | |
let doesNotExist = obj['doesNotExist']; // ok maybe it's not so bad | |
doesNotExist.toString(); // yikes | |
doesNotExist += 100; // double yikes | |
} |
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 { Hero } from 'third-party-library' | |
const hero: Hero = { name: 'codey' }; | |
function parseObject<T>(obj: Record<string, unknown>) { | |
return JSON.stringify(obj); | |
} | |
parseObject(hero); // Error |
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 Hero { name: string; } | |
const hero: Hero = { name: 'codey' }; | |
const generic: { [key: number]: any } = hero; // Error |
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 Hero { name: string; } // Still no string index signature | |
const hero: Hero = { name: 'codey' }; | |
const generic: { [key: string]: any } = hero; // Somehow still works with Record |
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 Hero { name: string; } // Missing the 'string' index signature | |
const hero: Hero = { hero: 'codey' }; | |
const generic: Record<string, any> = hero; // No Error. Why? How? |
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
const hero = { name: 'codey' } // object literal | |
const generic: Record<string, unknown> = hero; |
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 Hero { | |
name: string, | |
[key: string]: string // Manually add index signature | |
} | |
const hero: Hero = { name: 'codey' } | |
const generic: Record<string, unknown> = hero; |
NewerOlder