所以我想找到一种方法来拥有嵌套对象的所有键。
我有一个在参数中采用类型的泛型类型。我的目标是获取给定类型的所有键。
在这种情况下,以下代码运行良好。但是当我开始使用嵌套对象时,情况就不同了。
type SimpleObjectType = {
a: string;
b: string;
};
// works well for a simple object
type MyGenericType<T extends object> = {
keys: Array<keyof T>;
};
const test: MyGenericType<SimpleObjectType> = {
keys: ['a'];
}
Run Code Online (Sandbox Code Playgroud)
这是我想要实现的目标,但它不起作用。
type NestedObjectType = {
a: string;
b: string;
nest: {
c: string;
};
otherNest: {
c: string;
};
};
type MyGenericType<T extends object> = {
keys: Array<keyof T>;
};
// won't works => Type 'string' is not assignable to type 'a' | …Run Code Online (Sandbox Code Playgroud) 我正在使用 FormBuilder 创建表单。当我的一个 FormControl 更新时,我想调用另一个函数,所以我正在使用valueChanges它。但问题是我想在初始化它时触发它。我看到setValue应该可以做这些事情,但我看起来不像为我工作,或者我太糟糕了。
HTML:
<div class="col form-group">
<label for="stringArray">My label</label>
<select id="stringArray" class="form-control" *ngIf="stringArray$ | async as stringArray" formControlName="mySelect">
<option *ngFor="let val of stringArray" [value]="val">{{ val }}</option>
</select>
</div>
Run Code Online (Sandbox Code Playgroud)
TS
public myForm: FormGroup;
public stringArray$: Observable<string[]> = null;
constructor(private formBuilder: FormBuilder) {}
ngOnInit() {
this.initForm();
this.stringArray$ = this.myForm.get('mySelect').valueChanges.pipe(
switchMap(value => {
return this.doSomething(value);
})
);
this.myForm.get('mySelect').setValue('tmp', { emitEvent: true });
}
initForm() {
this.myForm = this.formBuilder.group({
mySelect: ['', Validator.required]
})
}
doSomething(value): Observable<string[]> { …Run Code Online (Sandbox Code Playgroud)