Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 488
Luiz Motta Avatar asked May 11 '26 16:05

Luiz Motta


1 Answers

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

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

seems to work just fine.

like image 72
joe Avatar answered May 14 '26 08:05

joe