为什么 class-transformer 演示中没有初始化器或 class 构造函数?
Why there are no initializers or class constructor in class-transofrmer demo?
import { Expose, plainToClass } from 'class-transformer';
class User {
@Expose() id: number;
@Expose() firstName: string;
@Expose() lastName: string;
}
const fromPlainUser = {
unkownProp: 'hello there',
firstName: 'Umed',
lastName: 'Khudoiberdiev',
};
这是来自 class-transformer's document 的演示。这是我的问题:User
不是无效的 Typescript Class,因为它没有任何属性或构造函数的初始值设定项吗?
事实上,我把这个demo复制到IDE后,立马得到了ts(2564): Property 'id' has no initializer and is not definitely assigned in the constructor
错误
”不是 User
无效的 Typescript Class”
如果
无效
{
...
"compilerOptions": {
...
"strict": true
...
}
...
}
如果
有效
{
...
"compilerOptions": {
...
"strict": false
...
}
...
}
在 tsconfig.json
.
--strict
Enable all strict type checking options. Enabling --strict
enables
--noImplicitAny
, --noImplicitThis
, --alwaysStrict
, --strictBindCallApply
, --strictNullChecks
, --strictFunctionTypes
and --strictPropertyInitialization
.
其实是strictNullChecks
.
import { Expose, plainToClass } from 'class-transformer';
class User {
@Expose() id: number;
@Expose() firstName: string;
@Expose() lastName: string;
}
const fromPlainUser = {
unkownProp: 'hello there',
firstName: 'Umed',
lastName: 'Khudoiberdiev',
};
这是来自 class-transformer's document 的演示。这是我的问题:User
不是无效的 Typescript Class,因为它没有任何属性或构造函数的初始值设定项吗?
事实上,我把这个demo复制到IDE后,立马得到了ts(2564): Property 'id' has no initializer and is not definitely assigned in the constructor
错误
”不是 User
无效的 Typescript Class”
如果
无效{
...
"compilerOptions": {
...
"strict": true
...
}
...
}
如果
有效{
...
"compilerOptions": {
...
"strict": false
...
}
...
}
在 tsconfig.json
.
--strict
Enable all strict type checking options. Enabling
--strict
enables--noImplicitAny
,--noImplicitThis
,--alwaysStrict
,--strictBindCallApply
,--strictNullChecks
,--strictFunctionTypes
and--strictPropertyInitialization
.
其实是strictNullChecks
.