在TypeScript 2.9.2中,我试图恢复函数声明的this类型。 这个是可能的吗? 这是我尝试过的: // Two different ways of specifying the type of `this`.
declare function foo(this: {hello: 'world'}): any;
type baz = ThisType<{hi: 'there'}> & (()=> any);
// Attempts to recover `typeof this` for foo and baz...
ty
如何在TypeScript 2.9.2中推断T的类型:
type Class<T extends object> = new(...args: any[]) => T
type Conditional<T> = T extends number ? Class<Number> : T extends string ? Class<String> : never
function<T>(conditional: Conditional<T>) {}
f(Number) // I want to infer T here
是否有任何方法(特殊语法)将类似rest参数的内容应用于TypeScript中的模板?
如果问题不明确,请看下面的示例和注释.
问题是-我能在这里申请像rest ...Types这样的程序吗?
function mixIt<A, B> (a: A, b: B): A & B;
function mixIt<A, B, C>(a: A, b: B, c: C): A & B & C;
/*
* ??
* If I would like to add D, E, F, etc, do I have to overwrite it like
为什么Typescript人员要创建infer关键字?根据的说法,这是一个如何使用它的示例:
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : any;
我不明白为什么需要这样做。为什么不能这样:
type ReturnType<T> = T extends (...args: any[]) => R ? R : any;
为什么这个不起作用?为什么infer关键字是必需的?
我正在寻求改进一些代码,我认为这是一个很好的表示使用类装饰器与Typescript的混合问题正是我正在寻找的,但解决方案“不可能”,我开始砍掉。
结果就是这个工作代码
declare type Constructor<T = {}> = new(...args: any[]) => T
//Permissions function runs when @Permissions is placed as a class decorator
export function Permissions<TBase extends Constructor>(Base:TBa
Flow有一个方便的,它可以获得调用类型F的函数的返回类型,第一个参数(可选)作为类型T,第二个参数等等。所以我们可以这样做:
type First = <T>(Array<T>) => T | void;
type ShouldBeOptionalString = $Call<First, Array<string>>; // ShouldBeOptionalString is string | void
TypeScript能实现同等的功能吗?我知道TypeScript提供的,但它不能根据参数类型计算返回类型:
type First =
我正在用TypeScript编写一个(玩具)散列和缓存装饰器,找不到一个好的方法来创建一个可靠的通用装饰器。
到目前为止,我掌握的代码是
function cache
(target: Object,
propertyKey: string,
// Likely we can do better than <any> here -- <Function<any>> maybe?
descriptor: TypedPropertyDescriptor<any>)
{
let cacheMap = new Map();
le
我想使用使用TypeScript将原始数据转换为User。我有两种不同类型的User:Employee和Customer。它们都扩展了基类,并且编译都没有问题。下面是这三个类的一个非常简化的版本。
abstract class User {
constructor(data: any) { }
static createFromServer<T extends User>(this: new (...args: any[]) => T, data: any): T {
return new this(data);
}
}
class Employee