![](/img/trans.png)
[英]How to merge multiple generic TypeScript lists into a new list with a union type?
我正在嘗試創建一個 TypeScript 方法,它將任意數量的通用列表合並到一個新列表中,其中新列表的類型是原始列表中項目類型的並集。 我有一個列表 class 如下: 我想向 List 添加一個 static 合並方法,它接受任意數量的 List 對象並將它們的項目合並到一個新的 List ob ...
[英]How to merge multiple generic TypeScript lists into a new list with a union type?
我正在嘗試創建一個 TypeScript 方法,它將任意數量的通用列表合並到一個新列表中,其中新列表的類型是原始列表中項目類型的並集。 我有一個列表 class 如下: 我想向 List 添加一個 static 合並方法,它接受任意數量的 List 對象並將它們的項目合並到一個新的 List ob ...
[英]How can I get Typescript to infer the correct type from a generic without using cast?
解釋器足夠聰明,可以推斷過濾器 function 的結果將是一個字符串數組,但它認為這不能分配給相同類型的變量 b? 有什么辦法可以解決這個問題而不必被迫這樣做 ...
[英]Why union generic constraint behaves weird in TS?
我想創建一個 function,它結合了 string|number arrays 的值。因此,在嘗試使用聯合類型擴展通用參數后,我得到了我不理解的奇怪行為。 在某些情況下,只有第一個項目的值被提取用於類型檢查,有時兩者都被提取。 這是一個例子 我希望 function 接受 arrays 個字符 ...
[英]Ensure a Typescript array has a specific value
我正在構建一個下拉式組件,值和選項是不同的道具。 我希望使用 Typescript 來確保該值是一個有效選項。 雖然我可以在運行時執行此檢查,但我覺得這是我應該能夠在 TS 中執行的操作。 這可能嗎?interface Props<N extends number> { coun ...
[英]Typescript forward generic type definition
考慮這種類型和這種泛型:type Data = { item: number }; type Generic<T> = { obj: T; }; 現在這是它的一個實例:const test: Generic<Data> = { obj: { item: 0 } }; ...
[英]Make function accept generic input, return value with same keys, but with values from another type
我有以下類型type A = { foo: number bar: number } type B = { foo: string bar: string } 我想編寫一個 function f() ,它接受一個完全或部分類型為 A 的參數,並輸出一個具有相同鍵但類型為 B 的類 ...
[英]How can I make a generic function that recursively deserializes JSON with nested datestrings type safe?
下面的代碼定義了一個 function stringsToDates ,它將采用原語 object 或 Promise 並遞歸地將它找到的任何 ISO8601 日期字符串(序列化 JSON)轉換為實例化的 Date 對象。 我怎樣才能使這樣的 function 類型安全,以便我可以調用const ...
[英]How to use Typescript generics when the argument could be a value or callback function
所以這里的問題是我想傳遞T ,它可以是string , number , boolean , object或array或function問題是我不知道如何在這種情況下處理ab("hello")並且有 T作為值返回。 這是我一直看到的錯誤並鏈接到錯誤 ...
[英]Typescript: how to make addiction from one field to another?
所以,我有select的接口。 我需要有類型的value 。 如果multiple等於true , value類型必須是string[]或string (如果multiple被禁用)。 成分: 我試過像這樣使用通用的 但我收到錯誤: 'multiple' implicitly has type ' ...
[英]Infer Object Generic from its propery (return function type)
我想制作一個可以從 function data的返回值推斷類型T的接口,以便 function step可以將this的類型作為參數接收。 像這樣: 但不必指定類型。 類似於 Vue.js (v3) 中使用data function 所做的,其中所有方法都可以訪問組件的 state 的成員 我知道 ...
[英]TypeScript types for Proxy with dynamic properties
我想輸入一個允許訪問任意屬性的 JavaScript Proxy 。 一個人為的例子,一個 object 將每個屬性映射到它自己的屬性名稱: 我不希望 TypeScript 自動推斷proxy.foo存在,但我想提供一種類型,使其知道它存在並且屬性的值類型取決於屬性名稱。 下面的映射類型至少可以防止 ...
[英]Typescript Generic to map an array of const objects to a list of types
我有一些 JSON 數據,我用as const斷言強類型化了這些數據。const myList = [ { type: 'uint256'}, { type: 'address'} ] as const; 現在我想將其轉換為以下元組類型:[number, string] 基本上,如果類型 ...
[英]Generic array type in Typescript
我有通用類型的 function。 但它返回錯誤Type 'boolean' is not assignable to type 'string'. 如何通過保持類型通用來解決此通用類型問題。 因為數組是未知的,值可能有不同的自定義類型。 ...
[英]Typescript switch map
我有以下帶有多個接口、組件和密鑰/接口 map 的 TS 代碼。interface FooProps { 'keyInFoo': string } const Foo = (props: FooProps) => {} interface BarProps { 'keyIn ...
[英]Is it possible to dynamically determine the type by the name of the object's proxy key in TypeScript
我正在編寫一個包裝器 class,它將包含與用戶實體交互的功能,例如檢查用戶是否為訪客的方法、檢查他的權限的功能等,以及我想為其字段創建訪問器的方法之一比如名字,email,id 。 我的想法是訪問者會通過實體中字段的名稱從用戶自己那里返回數據。 這是我的代碼和我想更改的注釋: 是否可以這樣安排,例 ...
[英]Typescript: Ensure all properties use the same tuple type
我有一個 function,其泛型是 1 個元素的元組或 2 個元素的元組。 我想確保 function 中使用的所有屬性都使用相同長度的元組。type TypeA = [string] // Tuple of 1 element type TypeB = [string, string] // T ...
[英]typescript class constructor inferring types based on args of its function as argument
抱歉,如果標題不是很清楚,但我不知道如何更好地表達我的問題。 我有一個Process class: 實例化新進程 Object 時: 我想對傳遞給 Message 構造函數的parameter進行類型檢查,以匹配 function 參數的類型。 就像在上面的代碼中,如果我傳遞一個數字而不是字符串,我 ...
[英]TypeScript generic array type predicate
我想要一個通用類型謂詞,它允許我檢查對象屬性的類型。 為了說明這一點,我想實現以下目標:const a: { [key in string]: string | string[]} = { arrayProp: ["this", "is", "an", "array"], otherProp ...
[英]Typescript autocompletion for conditionally returned function based on parameter of first function
我已經為此苦思冥想了一段時間,所以我決定在 Stackoverflow 上提出這個問題,希望有人能夠幫助我。 這是我的問題的簡化代碼片段版本: TypeScript Playground 解釋: 我希望在example function 使用特定option參數執行后自動完成。 因此,如果我鍵入e ...
[英]Typing based on subsets of unions in TypeScript
我正在開發一個 TypeScript 庫(這里是),用於代數數據類型(或任何人想稱呼它們的東西),並且正在努力處理一些更復雜的類型。 代數數據類型的工作方式如下:// Create ADT instantiator const value_adt = adt({ num: (value: n ...