我正在尝试在打字稿类中实现C# Class object initializer。但是编译器显示以下错误。
错误:输入“字符串|编号|空| undefined”不能分配给“ null”类型。错误:类型'undefined'不能分配给类型'null'
下面是代码
type gender = 'Male' | 'female';
class Person {
public name: string | null = null;
public age: number | null = null;
public gender: gender | null = null;
constructor(
param: Partial<Person>
) {
if (param) {
/**
* Is key exist in Person
* @param param key
*/
const keyExists = (k: string): k is keyof Person => k in this;
/**
* Keys
*/
const keys: string[] = Object.keys(param);
keys.forEach((k: string) => {
if (keyExists(k)) {
//k: "name" | "age" | "gender"
const value = param[k]; //value: string | number | null | undefined
//let a = this[k]; //a: string | number | null
this[k] = value; //error : Type 'string | number | null | undefined' is not assignable to type 'null'.
// Error : Type 'undefined' is not assignable to type 'null'.
}
});
}
}
}
let a = new Person({
age: 10
});
console.log(a);
和下面是tsconfig.json
{
"compilerOptions": {
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"strict": true, /* Enable all strict type-checking options. */
"strictNullChecks": true, /* Enable strict null checks. */
"baseUrl": "./", /* Base directory to resolve non-absolute module names. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
}
}
答案 0 :(得分:3)
您的问题实际上有两个方面:
param
是Partial<Person>
,因此它可能不包含所有存在的密钥。但是,keyExists()
方法仅会通知编译器Person
中是否存在给定密钥,因此您将最终以param[key]
返回undefined
来结束。value
的类型或缩小其范围。解决方案可能不是最佳解决方案,但这将是可行的:
param[k]
未定义,您需要添加类型保护检查this[k]
的类型强制转换为typeof value
尽管我通常避免使用手动类型转换,但在这种情况下,它是非常合理和安全的,因为在转换时,您正在使用k
,而该Person
必须是{{1}的成员}类,因此您只是在向编译器提示“我知道我使用的是哪种特定类型,不要惊慌”。
这样,Array.prototype.forEach
回调内部的修改后的逻辑如下所示:
keys.forEach((k: string) => {
if (keyExists(k)) {
const value = param[k];
if (value === void 0)
return;
(this[k] as typeof value) = value;
}
});
我个人不喜欢嵌套的if
语句,因为它使代码真的很难阅读,因此您也可以重构以上代码以仅使用保护子句:
keys.forEach((k: string) => {
if (!keyExists(k))
return;
const value = param[k];
if (value === void 0)
return;
(this[k] as typeof value) = value;
});