As we introduced "type" to both User and Admin it's now easier to distinguish between them. Once object type checking logic was extracted into separate functions isUser and isAdmin - logPerson function got new type errors.
Exercise:
Figure out how to help TypeScript understand types in this situation and apply necessary fixes.
// In case you are stuck: // https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
报错:
1 2 3 4
index.ts(52,40): error TS2339: Property 'role' does not exist on type'Person'. Property 'role' does not exist on type'User'. index.ts(55,40): error TS2339: Property 'occupation' does not exist on type'Person'. Property 'occupation' does not exist on type'Admin'.
As we introduced "type" to both User and Admin it's now easier to distinguish between them. Once object type checking logic was extracted into separate functions isUser and isAdmin - logPerson function got new type errors.
Exercise:
Figure out how to help TypeScript understand types in this situation and apply necessary fixes.