0
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 文档中的演示。这是我的问题:不是User无效的 Typescript 类,因为它没有任何属性或构造函数的初始化程序吗?其实我把这个demo复制到IDE后,马上就ts(2564): Property 'id' has no initializer and is not definitely assigned in the constructor报错了

4

1 回答 1

1

“不是User无效的打字稿类”

如果是无效的

{
    ...
    "compilerOptions": {
        ...
        "strict": true
        ...
    }
    ...
}

并且如果

{
    ...
    "compilerOptions": {
        ...
        "strict": false
        ...
    }
    ...
}

tsconfig.json.

--strict

启用所有严格的类型检查选项。启用会--strict启用 --noImplicitAny--noImplicitThis--alwaysStrict--strictBindCallApply--strictNullChecks和。--strictFunctionTypes--strictPropertyInitialization

[编译器选项]

它实际上是strictNullChecks

于 2021-03-15T11:19:04.393 回答