typescript-generics

    2热度

    1回答

    我不明白为什么需要打字原稿明确泛型类型定义为Child2和Child3在这种情况下: abstract class Base { public static A: string = "Fallback_A"; public DoSmthWithClassName(): string { return "Fallback_DoSmth"; } c

    0热度

    2回答

    我有以下的一般类的打字稿 type UserId = number type Primative = string | number | boolean class ColumnValue<T, S extends Primative> { constructor(public columnName: String, public value: S) { } } abstract

    1热度

    1回答

    我在这里简化了这个问题。 interface Identity{ name: string; } 另一个通用接口 interface State<T extends Identity>{ [T.name] : StateContainer<T> } 但因为它提供了错误这不会工作 才能找到t 如果我试图通过保持其内部产生的呢功能 function generate(c

    3热度

    1回答

    我想要一个函数,它需要一些对象并返回它的x属性。该对象需要被限制为通用类型Type<X>,我希望返回值的类型是属性x的类型。 要限制输入Type<X>我需要使用T extends Type<X>但我必须实际设置X像T extends Type<string>某种类型的值不会Type<number>或T extends Type<any>其丢弃x属性的类型信息的工作。 我希望能做一些像<T exte

    1热度

    1回答

    这是超出了TypeScript能够做的能力还是我做了一些非常错误的事情?下面是一些代码,我会想有工作: interface ISquare { area: number; } interface ICircle { radius: number; } abstract class Morph<T1, T2> { firstOption: T1;

    1热度

    2回答

    我认为这是一个正确实现泛型的问题,但我不确定。 我创建代表这里的问题Github的要点是: https://gist.github.com/ORESoftware/66b72b4b85262d957cb03ad097e4743e 说我有这个超: class A { foo(): A { return this; } } 和几个子类,一个例子看起来像

    4热度

    1回答

    我注意到我的项目有很多resolver尽管与不同的实体和存储库一起工作的服务托管了相同的代码。所以我接受了挑战,以减少他们使用泛型单解析服务: @Injectable() export class DetailResolver<T, R extends Repository<T>> implements Resolve<T> { constructor(private reposit

    1热度

    1回答

    因此,我发现在打字稿中有一个技巧,通过将类型映射到键值对,然后使用keyof类型创建可以是地图中的任何值的类型,将对象类型转换为区分的联合。下面是一个简单的例子: type SourceType = { foo: number, bar: string }; type MapWithKey<T> = {[P in keyof T]: { key: P, value: T[

    0热度

    1回答

    在VSCode尝试当这个打字稿遇到错误: enum Desg { Consultant, Engineer }; interface Employee { Name: string; Role: Desg; } let names: string[] = ['Sunit', 'Duke']; //Complains about Name pro

    0热度

    1回答

    目标 我最佳尝试只是执行打字的项目,让任何人都实现了一个动物必须提供此功能,并确保该功能只能采取两种Woof或Meow作为唯一参数,并且只能返回Woof或Meow。 初步尝试 下面是一些示例代码,这是类似的(除了名称)我在VSCode得到了代码(使用打字稿2.4.1): class Woof { constructor(private a: boolean = true) {} }