使接口中的所有字段以嵌套方式变为只读 [英] Make all fields in an interface readonly in a nested way

查看:145
本文介绍了使接口中的所有字段以嵌套方式变为只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,我们可以使用 Readonly< T> T 的所有字段重新声明为只读,例如:[打字稿:扩展接口并将现有字段重新声明为只读

I know that, we can use Readonly< T> to redeclare all fields of T as readonly, as in: [Typescript: extending an interface and redeclaring the existing fields as readonly

嵌套字段呢?例如:

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
}

如何创建SchoolReadonly类型,其中所有嵌套字段均为只读.

How to create a SchoolReadonly type, in which all nested fields are readonly.

一个简单的测试用例:

var s: SchoolReadonly = {
  teachers: [{teacherId: 1, personInfo: {name: "John", age: 40}}],
  students: [{studentId: 1, personInfo: {name: "Dan", age: 20}}]
}

s.teachers[0].personInfo.name = "John2"; //should produce readonly error
s.students[0].personInfo.age = 22; //should produce readonly error

作为约束,我不想将 Readonly 直接添加到School,Teacher,Student和PersonInfo接口.

As a constraint, I do not want to add Readonly directly to the School, Teacher, Student and PersonInfo interfaces.

推荐答案

您可以使用泛型并键入别名,这是很多代码,但是可以做到这一点:

You can use generics and type aliases, it's a lot of code, but it will do the trick:

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[0] = nul失败,则还需要将Array更改为ReadonlyArray,所以:

If you want s.teachers[0] = nul to fail then you need to also change the Array to ReadonlyArray, so:

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

是的,这不是一个简单的解决方案,而且非常冗长,但我认为至少到现在为止,还没有一个简单的通用解决方案可以解决该问题.
考虑:

True, it's not a simple solution and is very verbose, but I don't think that a simple generic solution exists, at least for now, for the problem.
Consider:

type ReadyonlyDeep<T> = {
    readonly [P in keyof T]: ReadyonlyDeep<T[P]>;
}

只要对象仅具有其他简单对象,此方法就很好:

This works great as long as the object has only other simple objects:

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

但是,如果您引入一个数组:

But if you introduce an array:

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

这可能不是一个容易解决的问题.

It probably isn't an easy problem to solve.

这篇关于使接口中的所有字段以嵌套方式变为只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆