Categories
TypeScript Answers

How to get argument types for a constructor or class with TypeScript?

Spread the love

Sometimes, we want to get argument types for a constructor or class with TypeScript.

In this article, we’ll look at how to get argument types for a constructor or class with TypeScript.

How to get argument types for a constructor or class with TypeScript?

To get argument types for a constructor or class with TypeScript, we can use the ConstructorParameters type with the typeof operator.

For instance, we write

type C = ConstructorParameters<typeof SomeClass>;

to create type C with ConstructorParameters and typeof.

We use typeof to get the data type for the SomeClass class.

And then we can use the returned type to get the type of the constructor parameters with ConstructorParameters and assign the returned type to C.

Conclusion

To get argument types for a constructor or class with TypeScript, we can use the ConstructorParameters type with the typeof operator.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.