我现在正在检查打字稿,偶然发现了一件奇怪的事。默认情况下,我使用的是通过npm安装的“typescript@1.5.0-alpha”。如果我有一个代码,declare是一个变量,然后将它用于赋值,那么所有代码都能正常工作。
.ts
declare var a;
var b = a;
.js
var b = a;
但是,如果我声明一个变量,然后尝试从它扩展一个类(假设该变量是一个有效的类型记录类标识符),类型记录就会失败,出现奇怪的错误,即找不到"name“:
.ts
declare var a;
class b extends a {}
误差
test.ts(2,17): error
我有一个这样的TypeScript接口: export interface Foo {
bar: string
} 这将编译成一个空的.js文件。因此,当在ES6节点模块中使用TypeScript接口时,我会得到以下错误: TypeError: Class extends value undefined is not a function or null 解决方法是创建一个类 export class BaseFoo {
bar: string
} 现在,我可以在ES6中使用BaseFoo,而不会出现上述错误。因此,在TypeScript中使用ES6互操作不可能只有接口,或者有没有比
为什么编译器隐式类型转换不适用于作为扩展接口实现的文字类型属性?
示例:
// 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
我正在通过实现来探索类型记录类型系统,在实现规范时遇到了一个问题。
规范规定Semigroup应遵循以下类型定义:
concat :: Semigroup a => a ~> a -> a
我理解这意味着实现Semigroup的类型Semigroup应该有一个concat方法,该方法接受a类型的参数,并返回一个a类型的参数。
在TypeScript中表达这种类型定义的唯一方法是:
interface Semigroup {
concat(other: this): this;
}
但是,当我试图在类上实现这个接口时,像这样:
class Sum implements
假设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
我正在使用spread操作符创建一个具有更改字段的副本,如下面的示例()所示。但是,如果在创建对象文本时更改了字段的类型,编译器就不会抱怨:
class A{
constructor(readonly a: number, readonly b: string[]){}
copy(): A { // must return an object of type A
return {
...this,
b: 99 // should be of type string[]!
}
}
}
con
我在Angular2组件中使用连接到Neo4j的请求时遇到CORS问题:
Response to preflight request doesn't pass access control check. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:8080' is therefore not allowed acces
我想将添加到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,我正在学习这个教程:https://blog.alexdevero.com/react-and-typescript-pt3/。 当他谈到接口时,他以以下方式在react中使用它: // Create interface for class component props
interface PropsInterface {
heading: string;
}
// Create interface for class component state
interface StateInterface {
firstName: string;
l
假设我有一个纯抽象类(即没有任何实现的抽象类):
abstract class A {
abstract m(): void;
}
与C#和Java一样,我可以扩展抽象类:
class B extends A {
m(): void { }
}
但与C#和Java不同的是,我还可以实现抽象类:
class C implements A {
m(): void { }
}
类B和C的行为是如何不同的?我为什么要选择一个对另一个?
(目前,TypeScript 和不包括抽象类。)
为什么下面的TypeScript代码会编译,但是systemjs不能在运行时正确地加载依赖项?
import { Observable } from 'rxjs';
let temp123 = new Observable<String>();
然而,这样做是可行的:
import { Observable } from 'rxjs/Observable';
let temp123 = new Observable<String>();
具体来说,第一个代码将生成一个包含以下代码的.js文件:
var Observable_1 = req
这个问题与角v.2+和TypeScript有关。
什么时候应该将输入作为单独的导入,以及当不需要时--我如何区分这两种情况?
在特定情况下,我在推荐之后安装了以下两个依赖项
npm install lodash --save
npm install @types/lodash --save-dev // <<< Do I need this as separate import or not ?!
如果我错了,请纠正我:
没有导入的类型,TypeScript仍然可以工作,但是IDE中关于方法的提示不能在导入类型的情况下工作吗?
许多库对于类型没有单独的导入,因为类
这是一个复杂的例子,但我不知道为什么TypeScript没有在这里抛出一个错误。
export class Square {
width: number;
}
export class MyShapeBox<T> {
shape: T;
}
var mySquareBox = new MyShapeBox<Square>();
var rectangle = { width: 3, height: 10 };
// Should this not fail?
mySquareBox.shape = rectangle;
根据我们的讨论,最好的猜
我有一个可以返回数字、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
然而,当我删除签名