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) {} }