我想将添加到TypeScript中的元素的scroll事件中,如下所示:
element.addEventListener("scroll", () => { }, { capture: true, passive: true });
但是TypeScript抛出一个语法错误:Argument of type '{ capture: true, passive: true }' is not assignable to parameter of type 'boolean'。
我知道错误的原因是TypeScript没有为addEventLi
我试图使用用TypeScript编写Node.js服务器,但在将fs.readFile转换为rxjs形式时遇到了错误。我希望下面的代码可以在TypeScript中工作
// This is a JavaScript example from the official documentation. It should
// also work at the TypeScript envrionment.
import * as fs from 'fs';
import { Observable } from 'rxjs';
let readFileAsObse
我正在尝试让Dockerode run typings与the implementation相匹配。问题是,当我调用run时,不管是什么,TypeScript都会将返回值视为Promise。 我认为问题在于TypeScript将function与Object相匹配,并且不能消除类型声明中run的重载的歧义。不过,我承认我有点迷惑了。 I've made a small example in a TypeScript Playground that I think exemplifies the error. // index.d.ts
declare class EventEmitt
我需要使用TypeScript重载一个方法。
FooModel有6个参数,但两个字符串参数是唯一的强制参数。因此,我不想每次想要使用myMethod时都创建一个,而是重载myMethod,并在那里创建FooModel,然后在返回之前创建其余的逻辑。
我已经根据到目前为止在网上发现的内容尝试了这一点,但是我得到了以下错误:
TS2394: This overload signature is not compatible with its implementation signature.
此错误的解决方案与我的方法是不一致的。
static async myMethod(model:
当传递给函数的参数数量无效时,我(仅)使用arguments抛出错误。 const myFunction = (foo) => {
if (arguments.length !== 1) {
throw new Error('myFunction expects 1 argument');
}
} 不幸的是,在TypeScript中,当在箭头函数中引用时,我得到错误The 'arguments' object cannot be referenced in an arrow function in ES3 and ES5. Consid
下面的代码将会失败。TypeScript会(错误地)认为返回类型是string,并允许您在其上调用charAt方法,而实际上它是一个number。
有没有办法改进代码,使TypeScript能够检测到错误?
function fn(s: number): number
function fn(n: string): string
function fn(arg: any): any { return 0 }
fn("hi").charAt(0)
在将项目从Visual Studio 2010迁移到2017年的过程中,我遇到了一个问题。其中一个项目具有全局静态重载的new/delete运算符,在使用VS2017编译时,它会抛出一个错误,即new/delete运算符不能声明为静态的(它用于在VS2010上编译)。但是静态确保特定于翻译单元的这些过载的新/删除的范围。当不允许更多的静态new/delete操作符时,如何实现这一点。请帮我提一下建议。
JavaScript函数parseInt可用于强制将给定参数转换为整数,无论该参数是字符串、浮点数、数字等等。
在JavaScript中,parseInt(1.2)将生成没有错误的1,但是在TypeScript中,它会在编译期间抛出一个错误:
error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
我是在这里遗漏了什么,还是TypeScript的预期行为?