mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-25 13:06:22 +00:00
621 B
621 B
date | id | title |
---|---|---|
20201126 | f8951d1c-bddf-49c3-bd61-fdfffc412deb | predicate |
Origin
Latin praedicatus "something declared"
Definition
A function that returns a single boolean value
Examples
type Predicate = () => boolean
function isTurkey(x: string) {
return x === 'turkey'
}
interface Dog {
bark: 'dog'
}
interface Cat {
meow: 'cat'
}
function isCat(animal): animal is Cat {
return (animal as Cat).meow !== undefined
}
function makeSound(animal: Cat | Dog) {
if (isCat(animal)) {
animal.meow
} else {
animal.bark
}
}