#What does is opeator/keyword do in TS?

13 messages · Page 1 of 1 (latest)

crystal trellis
#
function isFunction(value: unknown): value is Function {
  return typeof value === 'function';
}

I also don't understand how the return type is not just boolean looking at the return in the body.

limpid echo
#

It makes the thing passed into value a Function in the place where the function was called

#
declare const v: V
If (isFunction(v)) {
  // v is now V & Function
}
#

Although sometimes TS will do this automatically without you needing to write in that return type.

umbral pier
tiny sigilBOT
#
mjuksel#0

Preview:```ts
function ensureStringType(
value: any
): value is string {
return typeof value === "string"
}

let someNullish = "hey" as string | undefined
// ^?

let _length = someNullish.length
// 'someNullish' is possibly 'undefined'.

if (!ensureStringType(someNullish))
...```

hybrid cypress
#

!hb predicate

tiny sigilBOT
hybrid cypress
#

that function is a predicate ^, aka a type guard

#

@crystal trellis

crystal trellis
#

Are predicates useful while creating user defined type guard only?

hybrid cypress
#

predicates are typeguards

#

just different names