1

I'm new to typescript and I'm struggling with some basic syntax questions. Probably these are so basic that I wasn't able to find a good guide / tutorial that explains the stuff.

Here is the code:

export const createEntity = async <T extends EntityConstructor>(
  Constructor: T,
  input: Partial<InstanceType<T>>,
): Promise<InstanceType<T>> => {
  const instance = Constructor.create(input);
  return validateAndSaveEntity(instance as InstanceType<T>);
};
  1. what does < > do after the async keyword?
  2. I see that the code in ( ) after the < > keyword specify input variables and their types.
  3. what does < > do in Promise< >?

1 Answer 1

1
  1. <T extends EntityConstructor> is a type definition but the compiler declares the type based on given variables eg.
// type of output will be 'string'
let output = identity<string>("myString");  

or

// type of output will be 'string', the compiler will figure out `T`
// based on the value passed in
let output = identity("myString");  

or

// type of output will be 'number'
let output = identity(8675309);  

In your given Case that memans the <T extends EntityConstructor> will take most likely an object with the same parameters as EntityConstructor and can obtain other variables declared by you

  1. ON THIS IM NOT CERTAIN!!!

My take is that it will expect a function to be passed on through observable that will have or need those parameters.

  1. The Promise type is declared in < > so you could do Promise<string> and everthing that is not a string wont be passed on as Promise
Sign up to request clarification or add additional context in comments.

2 Comments

3. what do you mean by "Promise type"? I thought "Promise" is a type of object of its own. For example: variable x can be a "Promise object" or a "string object". What is a string Promise? Do you mean the return type of the Promise?
1. I can understand T extends EntityConstructor, what I don't get is how < > is related to the async keyword. Does async also has a type declaration?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.