Reputation: 315
I am new to typescript and trying to understand this following code Can someone please help and if there is any good course/book to learn typescript
type ConfigFactoryReturnValue =
| Record<string, any>
| Promise<Record<string, any>>;
export type ConfigFactory<
T extends ConfigFactoryReturnValue = ConfigFactoryReturnValue
> = () => T;
Upvotes: 0
Views: 74
Reputation: 3140
Although your question is a bit open-ended and broad still, I'll try to provide some information about the code you've shared.
// The type keyword is used to define a type, for example
type MyCustomType = string;
// Record<K, T> is a Utility type for handling objects.
// Record defines the type of key:value pair.
// This means that MyRecord is an object whose keys are string and can have any value
type MyRecord = Record<string, any>;
// Promise is used to determine a Promise type, which means a method which returns a Promise
// Promise<T> is the syntax in which T determines the type of returned value when promise is resolved
// ConfigFactoryReturnValue is a Type which can be an object of type string:any or promise<Object<string, any>>
type ConfigFactoryReturnValue = Record<string, any> | Promise<Record<string, any>>;
// The Type T extends ConfigFactoryReturnValue signifies that Generic type T must satisfy the type
// ConfigFactoryReturnValue i.e. along with other keys it should have keys defined in ConfigFactoryReturnValue
export type ConfigFactory<T extends ConfigFactoryReturnValue> = () => T;
as far as the Typescript course/book is concerned, there are plenty of courses available online on platforms like Udemy, Coursera, etc.
Upvotes: 2
Reputation: 312
The first block combined between type alias
and union type
, that means you defined your own data type name ConfigFactoryReturnValue
and that data type can be Record<string, any>
or Promise<Record<string, any>>
type ConfigFactoryReturnValue =
| Record<string, any>
| Promise<Record<string, any>>;
The second block combining type alias
with generics
feature of typescript.
That means exporting alias type
of a generic type.
<T extends ConfigFactoryReturnValue = ConfigFactoryReturnValue>
define that the type T
should extend from ConfigFactoryReturnValue
or is instance of ConfigFactoryReturnValue
as default.
export type ConfigFactory<
T extends ConfigFactoryReturnValue = ConfigFactoryReturnValue
> = () => T;
Upvotes: 1