打字稿:扩展接口并将现有字段重新声明为只读 [英] Typescript: extending an interface and redeclaring the existing fields as readonly
本文介绍了打字稿:扩展接口并将现有字段重新声明为只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我们有一个这样的界面:
Let's say we have an interface like this:
interface Person {
name: string;
age: number;
}
我想调用 Readonly 并创建一个只读版本的界面,例如
I want to call Readonly and create a readonly version of the interface, e.g.
interface PersonReadonly extends Readonly<Person> {}
这将等同于写作
interface PersonReadonly {
readonly name: string;
readonly age: number;
}
我们能不能写一个这样的Readonly泛型接口,还是已经写好了?
Can we write such a Readonly generic interface, or is it written already?
推荐答案
您可以:
type PersonReadonly = Readonly<Person>
但它不是一个接口.例如,您不能在其他地方添加新成员.
But it is not an interface. For example, you can't add a new member somewhere else.
2017 年 5 月自 TS 2.2(2017 年 2 月)起,接口可以从类型派生.
这篇关于打字稿:扩展接口并将现有字段重新声明为只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文