键值对声明的Typescript数组 [英] Typescript array of key value pairs declaration

查看:90
本文介绍了键值对声明的Typescript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对以下声明感到困惑:

constructor(controls: {[key: string]: AbstractControl}, optionals?: {[key: string]: boolean}, validator?: ValidatorFn, asyncValidator?: AsyncValidatorFn)

控件的类型是什么(第一个参数)? 它是一个键值对数组的对象,其中键是字符串,值是AbstractControl?谢谢!

What is the type of the controls (first parameter)? Is it an object which is an array of key value pairs where key is string and value is AbstractControl? Thanks!

推荐答案

是的,就像您猜到的那样,它是一个js对象,键为字符串,AbstractControl为值.
例如:

Yes, like you guessed, it's a js object with key as string and AbstractControl as values.
For example:

{
    "control1": new Control(),
    "control2": new Control()
}


编辑

您可以通过两种方式将变量声明为这种类型:


Edit

You can declare a variable to be of this type in two ways:

let controls: { [key: string]: AbstractControl };

interface ControlsMap {
    [key: string]: AbstractControl;
}

let controls: ControlsMap;

甚至更好:

interface ControlsMap<T extends AbstractControl> {
    [key: string]: T;
}

let controls1: ControlsMap<AbstractControl>;
let controls2: ControlsMap<MyControl>;

这篇关于键值对声明的Typescript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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