Reputation: 31
I read this article and I don't quite understand a real world use case and what I can use typeof for. I realise they're for anonymous types but does anyone have any ideas for real world use case?
Thank you!
Here's the code from the article for quick viewing.
let rectangle1 = { width: 100, height: 200 };
// Obtain the type of `rectangle1` and call it `Rectangle`
type Rectangle = typeof rectangle1;
let rectangle2: Rectangle;
Upvotes: 0
Views: 1124
Reputation: 249666
There are multiple use cases for this.
Sometimes as in your example the object is already defined as an object literal, and that you don't to explicitly define the type for it but you do want to be able to pass it around in a type safe way to methods:
let rectangle1 = { width: 100, height: 200 };
type Rectangle = typeof rectangle1;
function clone (r: Rectangle ): Rectangle {
return r;
}
Or sometimes there is a variable/function exported by a module but is not publicly exported, you can use typeof
to have a named type for the variable, as an example, i just used it in this answer
import * as ts from 'typescript' // Import will be elided as long as we only use types from it, so we don't have the compiler code loaded at runtime
type CompilerOptions = typeof ts.parseCommandLine extends (...args: any[])=> infer TResult ?
TResult extends { options: infer TOptions } ? TOptions : never : never;
type TypeAcquisition = typeof ts.parseCommandLine extends (...args: any[])=> infer TResult ?
TResult extends { typeAcquisition?: infer TTypeAcquisition } ? TTypeAcquisition : never : never;
typeof
can be used even without a named type, for example you could define the type of a parameter the same as the type of another parameter (avoiding writing a type twice, especially if it is long)
function asPromise(value: string| number| Date) : Promise<typeof value> { return null as any; }
The use cases are endless, these are just examples.
Upvotes: 1
Reputation: 1467
For example you want to pass only proper type to filter, you may have such construction.
const equals:Operator = (value: string, filterValue: string) => {
return value == filterValue;
};
const startsWith:Operator = (value: string, filterValue: string) => {
return value.startsWith(filterValue);
};
const contains:Operator =(value: string, filterValue: string) => {
return !!value.match(filterValue);
};
const endsWith:Operator = (value:string, filterValue: string) => {
return value.endsWith(filterValue);
};
const regex:Operator = (value: string, filterValue: RegExp) => {
return filterValue.test(value);
};
const StringOperatorsMap = {
//equals
'=': equals,
'eq':equals,
'equals':equals,
//contains
'contains':contains,
'*':contains,
//match
'regex': regex,
'match': regex,
're': regex,
//startsWith
'startsWith':startsWith,
'^':startsWith,
//endsWith
'endsWith':endsWith,
'$':endsWith
};
export type StringOperators = keyof typeof StringOperatorsMap;
export const getOperator = (operator: StringOperators) => {
return StringOperatorsMap[operator];
};
Upvotes: 0