是否有关于如何设置默认属性值的样式指南?最好官方发布。
class MyClass {
public foo = 'asdf';
}
并且
class MyClass {
public foo: string;
constructor() {
this.foo = 'asdf';
}
}
两者都编译为
var MyClass = /** @class */ (function () {
function MyClass() {
this.foo = 'asdf';
}
return MyClass;
}());
第一种写法更加简洁,类似于C#语言。而第二种写法更接近编译输出的语法(如果有定义文件的话),属性类型也更明确。
我查看了以下网站:https://www.typescriptlang.org/docs/home.html、https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines 和 https://angular.io/guide/styleguide。
tslint.json
中得到了来自tslint的[tslint] Type string trivially inferred from a string literal, remove type annotation (no-inferrable-types)
的警告,但是我可以添加ignore-properties
到 https://palantir.github.io/tslint/rules/no-inferrable-types/,谢谢。 - William Lohan