castTo
Generic types: | T |
Casts a value to a given type. This is instead of using:
interface Foo {
bar: string;
}
const foo: Foo = { bar: 'baz' };
// cast foo to a Record<string, string> so we
// can access bar as a string index
const bar = foo as unknown as Record<string, string>;
const barVar = bar['bar'];
Usage:
interface Foo {
bar: string;
}
const foo: Foo = { bar: 'baz' };
// cast foo to a Record<string, string> so we
// can access bar as a string index
const bar = castTo <Record<string, string>>(foo);
const barVar = bar['bar'];
Presentation
function castTo (value: unknown): T;
Returns
T
-the value cast as T
Parameters
Name | Type | Description |
---|---|---|
value | unknown | the value to cast |