To address the issue mentioned in the feedback, it is important to ensure that Student
extends Partial<Gender>
rather than Gender
. The error that follows serves as a precautionary measure to avoid inheriting potentially unrelated types for the same attribute. To eliminate this error, you can explicitly include the specified property so that the compiler can resolve it:
export interface Student extends Partial<Gender>, Human { };
export interface Peter extends Student, Pick<School, 'address'>, Gender {
gender: boolean; // valid
};
Another approach to handle this without duplicates is by utilizing intersection types instead of extends
. Although similar, if A extends B, C
is true, then A extends B & C
should also hold true. However, due to syntax restrictions limiting implements
or extends
clauses in type declarations to named types with known property names:
export interface Student extends Partial<Gender>, Human { };
export interface Peter extends Pick<School, 'address'>, (Student & Gender) { }; // encountered an error!
// --------------------------------------------------> ~~~~~~~~~~~~~~~~~~
// An interface can only extend an identifier/qualified-name with optional type arguments.
Fortunately, since Student & Gender
has statically known properties, providing it with a name resolves the issue:
export type GenderedStudent = Student & Gender;
export interface Peter extends Pick<School, 'address'>, GenderedStudent { }; // no errors
This implementation works smoothly.
In both scenarios presented, Student
does not necessitate Gender
, whereas Peter
requires it (oddly enough representing individuals named "Peter"):
const s: Student = {
first: "Harry",
last: "Potter"
}; // valid
const p: Peter = {
first: "Peter",
last: "Pettigrew",
address: "Gryffindor House, Hogwarts",
gender: true // correct as Peter possesses a gender
}; // valid
const badP: Peter = {
first: "Peter",
last: "Pettigrew",
address: "Ron Weasley's pocket",
}; // incorrect, missing gender
These insights are intended to assist you in resolving your concern. Good luck!
Access the playground link for code demonstration