我正在尝试用JavaScript重写一个有用的TypeScript类。在我的JavaScript类中,我使用闭包变量self实现私有字段。它在事件处理程序中很有用,例如onkeypress,在那里了解事件调用程序和类的当前实例非常重要。
var MyClass = function (valueHolder) {
var self = this;
this.valueHolder = valueHolder;
MyClass.prototype.DoUsefullactions = function (value) {
如何使用JSDoc注释向TypeScript编译器声明“类型X具有Y类型的所有属性,以及这些其他属性”?
给予:
/**
A base type with one property.
@typedef {object} Base
@prop {string} name
*/
/**
A subtype that adds a second property.
@typedef AugmentedBase
@type {Base}
@prop {number} age
*/
/**
A function declared to return AugmentedBase, so name
有时,typescript会使用以下代码生成定义文件,这会导致编译错误: // test.ts
import { Subject } from 'rxjs';
class Test {
get property() {
return new Subject().asObservable();
}
}
// test.d.ts
class Test {
property: import("rxjs").Observable<any>;
} 为什么会发生这种情况,以及如何捕获这些类型的错误?
我正在尝试使用中的代码。它使用反射。这里有一份副本:
export function CustomComponent(annotation: any) {
return function (target: Function) {
var parentTarget = Object.getPrototypeOf(target.prototype).constructor;
var parentAnnotations = Reflect.getMetadata('annotations', parentTarget);
var parentAnnota
我有一个构建链设置,将一个文件从coffeescript到typescript再到javascript。我的问题是:将类型签名添加到coffeescript函数的最小干扰方式是什么?
coffeescript通过反引号支持原始javascript。然而,这意味着coffeescript不再能理解反引号片段。
Coffeescript拒绝这些:
f = (`a:String`) -> a + 2
f = (a`:String`) -> a + 2
我可以在函数上面写这段代码:
`var f = (String) => any`
它会编译,但不会进行类型检查。我认为这是因为Cof
有人能解释一下为什么当把1.0传递给javascript中的函数时,它被转换为1,以及如何解决这个问题?
var return_me = function(value) {
return value;
}
console.log("1.0 is returned as " + return_me(1.0));
声明文件有问题。我试图为d.ts框架编写openUi文件。它们对其属性使用get()和set(var)语法。
在JS中,我将有以下设置:
sap.ui.getCore().attachInit(function () {
var text = new sap.m.Text();
text.setText("Hello World!"); // sets the text property
text.placeAt("content", 0);
});
在打字稿中,我希望有这样的getter和setter语法:
var textcontro
JavaScript和TypeScript中的许多导入表单都具有导出parallels,从而可以轻松地重新导出值和类型。例如。
export { A, B } from './module'
或
export { default as Name } from './module'
Ohers没有,原因对我来说是有意义的。例如。
export Name from './module'
这是没有意义的,因为我们没有指定我们想要在哪个名称下重新导出Name。
但为什么这不起作用:
export * as Name from './module&
我试图显示一个饼形图,它实际上是工作的,即使我得到了TypeScript编译错误:Argument of type '(d: any) => {}' is not assignable to parameter of type 'datum: Arc<number>, index: number, outerIndex: number) => number | string | boolean'. Type '{}' is not assignable to type 'number | string | bool
如果我创建一个名为TypeScript module的简单test,则如下所示:
module test
{
export class MyTest {
name = "hello";
}
}
生成的JavaScript创建一个IIFE,如下所示:
var test;
(function (test) {
var MyTest = (function () {
function MyTest() {
this.name = "hello";
}
ret