![](/img/trans.png)
[英]infer type from constructor options
這行得通,但我確信有更好的方法可以做到這一點,我正在使用 zod 對我對用戶零控制的值進行模式驗證。 然后我有一個非常簡單的 class,它采用這些模式並執行驗證,並將類型推斷回傳遞給“Action”的方法。 然后我這樣使用它: 這有效,輸入和值驗證器上的智能感知是正確的,但是我不禁認為有一種更好的 ...
[英]infer type from constructor options
這行得通,但我確信有更好的方法可以做到這一點,我正在使用 zod 對我對用戶零控制的值進行模式驗證。 然后我有一個非常簡單的 class,它采用這些模式並執行驗證,並將類型推斷回傳遞給“Action”的方法。 然后我這樣使用它: 這有效,輸入和值驗證器上的智能感知是正確的,但是我不禁認為有一種更好的 ...
[英]How do I have typescript have the type properly infered when using a generic initializer function?
我有一個 function 可以采用這樣的可調用形式的參數:type Initial<T> = T | PromiseLike<T> | (() => T) | (() => PromiseLike<T>) const usePromise = & ...
[英]Typescript type parameter not inferred from generic class as expected
在此示例中,我使用參數類型(keyof TFoo)[]來期望包含TFoo中的任何鍵的數組,以保持示例簡單,但我在任何時候都看到類型參數推斷的相同行為使用模式<T, TSomeClass extends SomeClass<T>> 。 它似乎正確地推斷出TFoo類型,因為它適用 ...
[英]Is it possible for a Swift type to be inferred by "pulling out" a Type value from a generic function's parameter?
介紹 (如果標題令人困惑,我深表歉意,但我在這里更好地解釋了這個問題!) 我正在構建一個可以對其響應執行 JSON 解碼的網絡庫。 采用此庫的主機應用程序將創建符合NetLibRoute的枚舉。 當前所做的只是強制存在asURL : 在主機應用程序中,我有一個路由系統,它在編譯器級別(通過枚舉和關 ...
[英]Bind wildcard type argument in Scala
在 Scala 2 中,您當然可以使用通配符或存在類型作為類型 arguments。但是,這意味着您並不總是有想要使用的類型的名稱。 這有時會導致奇怪的情況,您需要依靠類型推斷來規避顯式編寫類型。 這是我的意思的一個有點人為的例子:case class Container[T](value: T) ...
[英]Can I get type inference on multiple variables through a single type predicate?
我想弄清楚是否有一種方法可以將map應用於類型謂詞,以便能夠替換以下代碼片段中的重復調用(每個變量一個),以便 TSC(仍然)知道 A 和 B 是可迭代對象(集合是): isSet當然是類型謂詞: 我嘗試用檢查[A,B]是否為數組Set<unknown>[]的東西替換它,但這似乎不適 ...
[英]Inferring the type of the return of a function from one of the inputs to that function
在我們的應用程序中,我們定義了一個枚舉,它涵蓋了應用程序可以訪問的所有后端端點...... 這些被翻譯成單獨的端點和參數等。 所以我們在 ApiClient 上有幾個像這樣的函數,最終會調用 .network ...... 如您所見,這些將返回的數據解碼為特定的通用類型。 所以在呼叫站點它看起來像其 ...
[英]Are assignments implicitly covariant?
這一定是我的一個非常基本的誤解。 參數類型的分配似乎是協變的,但我沒有任何跡象表明這就是我想要的。 為了簡潔起見,我粘貼了 Scala 代碼,但它在 Java 中的行為相同。class Pet class Fish extends Pet class Guppy extends Fish case ...
[英]Type inference in Kotlin lambdas fails when using `it` special variable
我不明白,為什么要編譯以下內容: 但這不是: 我是否正確地假設在第一種情況下, it的類型是通過name屬性推斷出來的? 為什么在第二種情況下沒有發生這種情況? ...
[英]If the condition `property in object` is true, why does TypeScript not infer the type of object[property] to be the union type of object's values?
我正在嘗試訪問object[property] ,而 TypeScript 正在拋出該element implicitly has any type錯誤。 如果我們不知道property in object是否為真,我們必須自己提供帶有索引簽名的 TypeScript。 也就是說,如果我們從 fu ...
[英]C: how to return a struct literal without mentioning its type name?
我們可以通過將匿名文字初始值設定項轉換為返回類型來從 function 返回結構文字: 是否可以在初始化程序的任何地方不提及struct foo ? 單獨聲明: 不會做,因為它仍然明確提到struct foo類型。 是否可以使用typeof獲取包含 function 的返回類型? 我試過了: re ...
[英]TypeScript: How to type an object with properties that are added later?
我有這個基本layers object,我想稍后向每個層添加東西。 這是我向每一層添加東西的方法: 這會導致 2 個預期錯誤 我可以想到兩種方法來解決這個問題,但這兩種解決方案都有我想避免的缺點。 首先,我可以在我的layers object 中更加明確: 缺點:我必須寫很多額外的行。 我能想到的 ...
[英]Why the ReturnType, one of the TypeScript's utility types returns `any` when the `inter R not equal to R`
在TypeScript 官方文檔上,type MyReturnType<T extends (...args: any) => any> = T extends ( ...args: any ) => infer R ? R : any; // this 'an ...
[英]Why creating a local binding with Borrow in Rust makes inferring the type impossible when using borrow() on RefCell?
我嘗試從 RefCell 借用: 它有效,但這個無效: 並且錯誤消息沒有幫助:error[E0283]: type annotations needed for `&Borrowed` --> src/bin/a.rs:404:19 | 404 | let kk ...
[英]Is there a way to specify the return type of a object function to the React Prop type of another object record?
我想做的是定義一個像這樣的類型 其中兩個使用的any是相同的。 所以傳遞給component的 Component Prop Type 應該是傳遞給props的 function 的返回類型。 我認為泛型在這種情況下可能會有所幫助,但不知道將它放在哪里,因為泛型類型應該特定於layoutName的 ...
[英]Why can't Swift infer this return type properly?
我正在嘗試做一些我認為應該非常簡單的事情,但是我在 Swift 的類型推斷方面遇到了麻煩。 我真的不明白為什么它會掉在這里。 我有一個Cocktail類型,它有其他屬性,但這里唯一重要的是name :struct Cocktail { // ... other stuff let ...
[英]How to validate generic variadic mapped tuple arguments without losing the inferred generic types?
我有一個高階函數createHandler ,它以可變映射元組的形式接受剩余參數,將它們映射到通用對象類型(我們現在稱它為ObjA<> )。 然后可以將返回的函數傳遞給另一個高階函數useHandler ,該函數將調用它(顯然在此過程中做更多事情)。 我斷言從createHandler ...
[英]Scala 2 type inference
def test[T: ClassTag]: T = { println(classTag[T]) null.asInstanceOf[T] } val x1: Int = test val x2: Int = test[Int] 印刷 我希望編譯器猜測Int類型而不需要顯式提供它(編輯: ...
[英]Why doesn't generic type inference infer a union from two sites?
考慮 這是 TypeScript 4.9 中的一個錯誤,即 boolean 類型的參數不能分配給 bigint 類型的參數很明顯,TS 在調用中推斷 T 是 bigint,從而使調用無效。 但是 T 有一個非常好的類型可以使調用合法,即bigint | boolean bigint | bool ...
[英]Is there a way to elide type of a composite literal by inferring types based on the type parameter of function in go?
type Foo struct { a string b int } type Bar[K Foo|string] struct { c K d string } func someRandomFunc (x []Bar[Foo], y [ ...