Доки по разработке
This project is maintained by teniryte
asserts)Принципы
asserts value is Type гарантирует, что после успешного вызова значение имеет указанный тип.Пример
interface Person {
firstName: string;
surname: string;
}
interface Organisation {
name: string;
}
type Contact = Person | Organisation;
function assertIsPerson(contact: Contact): asserts contact is Person {
if ((contact as Person).firstName === undefined) {
throw new Error('Not a person');
}
}
function sayHello(contact: Contact) {
assertIsPerson(contact);
console.log('Hello ' + contact.firstName);
}
Практика
sayHello3 → sayHello).