Luiz Motta
Luiz Motta

Reputation: 31

Typescript - Require generic type on function without parameter

Is it possible to require a generic type on a function without parameters?

Example:

function myFunc<T>() {
  return {} as T
}

const a = myFunc() // a: unknown

I want to make the generic type required, but couldn't find anything about it.

Upvotes: 3

Views: 2472

Answers (1)

joe
joe

Reputation: 1489

function myFunc<T>() {
  return {} as T
}

const a = myFunc<SomeObject>() // a:SomeObject

seems to work just fine.

Upvotes: 3

Related Questions