对象扩展广泛用于更新功能样式的旧对象,如下所示:
let o1 = { x: 5, y: 6 };
let o2 = { ...o1, y: 10 };
alert(JSON.stringify(o2));
在新的TypeScript 2.1中,我们现在已经实现了对象扩展,但与简单的对象文本不同,它允许具有未知属性:
interface Point { x: number, y: number }
let o1: Point = { x: 5, y: 6 };
//ERROR: Object literal may only specify known properties
let o2:
根据这篇文章
您应该能够使用以下代码创建一个EventError:
var error = new ErrorEvent('oh nose', {
error : new Error('AAAHHHH'),
message : 'A monkey is throwing bananas at me!',
lineno : 402,
filename : 'closet.html'
});
但是,如果您在TypeScript操场上尝试此操作:
上面写着Supplied parameters do
我想将主机对象Error扩展到一个自定义的UploadError类。下面的示例在我编译时失败:
class UploadError extends Error {
constructor(message: string, private code: number) {
super(message);
}
getCode(): number {
return this.code;
}
}
当我运行TypeScript编译器tsc时,我得到以下错误:
UploadError.ts(1,0): A export class may o
假设TypeScript的以下片段:
class Item {
name: KnockoutObservable<string>;
}
class Editor {
items: KnockoutObservableArray<Item> = ko.observableArray([]);
itemInEditMode: KnockoutObservable<Item> = ko.observable(null);
constructor() {
var x = new Item();
t
我有一个可以返回数字、Error或NegativeError的函数
class NegativeError extends Error {
name = 'NegativeError'
}
function doThing (a: number): number | NegativeError | Error {
return a < 0 ? new NegativeError() : a === 0 ? new Error() : a
}
const done = doThing() // T | NegativeError | Error
然而,当我删除签名
我有一个Vue.js 2项目,我要移植到TypeScript (一次有点)。我试图让ESLint解析我的TypeScript文件和Vue组件,但是当我运行vue-cli-service lint时,所有的.vue文件都报告了这个错误:
error: Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: src/path/to/Foo.vue.
The file must be i
我如何在plugin:@nrwl/nx/typescript中过激规则?我对.eslintrc.json的根做了这个更改。
"rules": {
"@typescript-eslint/member-ordering": "warn"
},
在引入违反规则的演示之后,仍然会出现错误。
D:\me\sample\apps\my-app\src\app\app.component.ts
16:3 error Member outOfOrder should be declared before all instance method de
在TypeScript中,如何从基类本身获取子类(调用基类的构造函数)的名称。我将代码设置为:
class Animal{
constructor(){ console.log(this.constructor.name)} //this is throwing error}
}
class Cow extends Animal{
constructor(){ super() }
}
new Cow() // this should log "Cow"
在plane中,Js this.constructor.name曾经有效,但在TypeScript中似乎不是这样。我
为什么编译器隐式类型转换不适用于作为扩展接口实现的文字类型属性?
示例:
// typescript 3.4.5
interface INumContainer {
num?: 1 | 2;
}
class myClass implements INumContainer {
// does not compile
// error TS2416: Property 'num' in type 'myClass' is not assignable to the same property in base type 'INumContai
我有一个git存储库,我应该使用命令react-scripts start启动它。当我运行这个命令时,我得到了这样的响应:无法找到typescript: react-scripts start
It looks like you're trying to use TypeScript but do not have typescript installed.
Please install typescript by running npm install typescript.
If you are not trying to use TypeScript, please remov