November 2018
Beginner
502 pages
10h 22m
English
In situations where we can't use the other type guards, we can create our own. We can do this by creating a function with the return type as type predicate. We actually used a user-defined type guard earlier in the book when we went through the unknown type.
Let's implement the example from the last two sections using our own type guard function:
interface IPerson { id: number; firstName: string; surname: string;}interface ICompany { id: number; name: string;}type PersonOrCompany = IPerson | ICompany;
function isPerson(personOrCompany: PersonOrCompany): personOrCompany ...
Read now
Unlock full access