Typescript 以嵌套方式将接口中的所有字段设置为只读
我知道,我们可以使用ReadonlyTypescript 以嵌套方式将接口中的所有字段设置为只读,typescript,nested,readonly,readonly-attribute,Typescript,Nested,Readonly,Readonly Attribute,我知道,我们可以使用Readonly将T的所有字段重新声明为Readonly,如下所示:[ 嵌套字段呢?例如: interface School { teachers: Array<Teacher>; students: Array<Student>; } interface Teacher { teacherId: number; personInfo: PersonInfo; } interface Student { studentId: n
interface School {
teachers: Array<Teacher>;
students: Array<Student>;
}
interface Teacher {
teacherId: number;
personInfo: PersonInfo;
}
interface Student {
studentId: number;
personInfo: PersonInfo;
}
interface PersonInfo {
name: string;
age: number
}
作为约束,我不想直接将Readonly添加到学校、教师、学生和PersonInfo界面。您可以使用泛型和类型别名,这需要大量代码,但它可以实现以下功能:
interface School<T extends Teacher<PersonInfo>, S extends Student<PersonInfo>> {
teachers: Array<T>;
students: Array<S>;
}
type EditableSchool = School<EditableTeacher, EditableStudent>;
type ReadonlySchool = Readonly<School<ReadonlyTeacher, ReadonlyStudent>>;
interface Teacher<P extends PersonInfo> {
teacherId: number;
personInfo: P;
}
type EditableTeacher = Teacher<PersonInfo>;
type ReadonlyTeacher = Readonly<Teacher<Readonly<PersonInfo>>>;
interface Student<P extends PersonInfo> {
studentId: number;
personInfo: P;
}
type EditableStudent = Student<PersonInfo>;
type ReadonlyStudent = Readonly<Student<Readonly<PersonInfo>>>;
interface PersonInfo {
name: string;
age: number
}
var s: ReadonlySchool = {
teachers: [{teacherId: 1, personInfo: {name: "John", age: 40}}],
students: [{studentId: 1, personInfo: {name: "Dan", age: 20}}]
}
s.teachers = null;
s.teachers[0].personInfo.name = "John2"; // Cannot assign to 'name' because it is a constant or a read-only property
s.students[0].personInfo.age = 22; // Cannot assign to 'age' because it is a constant or a read-only property
其余的都一样,但是:
s.teachers = null; // Cannot assign to 'teachers' because it is a constant or a read-only property
s.teachers[0] = null; // Index signature in type 'ReadonlyArray<Readonly<Teacher<Readonly<PersonInfo>>>>' only permits reading
然后:
这可能不是一个容易解决的问题。难道没有更简单、更通用的方法吗?可能是重复的
interface School<T extends Teacher<PersonInfo>, Ta extends ArrayLike<T>, S extends Student<PersonInfo>, Ts extends ArrayLike<S>> {
teachers: Ta;
students: Ts;
}
type EditableSchool = School<EditableTeacher, Array<EditableTeacher>, EditableStudent, Array<EditableStudent>>;
type ReadonlySchool = Readonly<School<ReadonlyTeacher, ReadonlyArray<ReadonlyTeacher>, ReadonlyStudent, ReadonlyArray<ReadonlyStudent>>>;
s.teachers = null; // Cannot assign to 'teachers' because it is a constant or a read-only property
s.teachers[0] = null; // Index signature in type 'ReadonlyArray<Readonly<Teacher<Readonly<PersonInfo>>>>' only permits reading
type ReadyonlyDeep<T> = {
readonly [P in keyof T]: ReadyonlyDeep<T[P]>;
}
interface A {
b: B
}
interface B {
str: string;
}
let a: ReadyonlyDeep<A>;
a.b.str = "fe"; // Cannot assign to 'str' because it is a constant or a read-only property
interface B {
str: string;
moreB: B[];
}
a.b.moreB = []; // Cannot assign to 'moreB' because it is a constant or a read-only property
a.b.moreB[0].str = "hey"; // this is fine though