#What does is opeator/keyword do in TS?
13 messages · Page 1 of 1 (latest)
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.
Take a look at the playground here @crystal trellis https://tsplay.dev/wg179m
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))
...```
You can choose specific lines to embed by selecting them before copying the link.
!hb predicate
Handbook / Narrowing
Are predicates useful while creating user defined type guard only?