Because of `strictNullChecks` return type of function is `<type> | undefined` although it will never be undefined

27 Views Asked by At

The function is as follows:

const getProp = <
  TObj extends object,
  TKey extends keyof TObj
>(props: TObj, key: TKey, def: TObj[TKey]): TObj[TKey] => {
  if (props[key] == null) return def
  return props[key]
}

When used as follows:

const fluid = getProp(props, 'fluid', true) // return type is boolean | undefined

The return type is boolean | undefined although I'd expect and need it to be boolean.

0

There are 0 best solutions below