generics

Typescript: infer type of generic after optional first generic

心不动则不痛 提交于 2020-12-04 08:22:26
问题 I have a function with two generic types, In and Out : function createTask< In extends Record<string, any> = {}, Out extends Record<string, any>, >(task : TaskFunction<In, Out>) : Task<In, Out> type TaskFunction<In, Out> = (args : TaskWrapper<In>) => Out | Promise<Out>; // TaskWrapper wraps several other types and interfaces, so args is more than just `In` This code currently does not compile, because you cannot have a required generic type ( Out ) after an optional one ( In ). How do I tell

How to add constraints on generics

二次信任 提交于 2020-12-04 00:43:26
问题 I'm having trouble finding out how I constrain the generic types. It seem like K need to implement the core::cmp::Eq and core::hash::Hash traits. I've been unable to find the required syntax in the docs. use std::collections::HashMap; struct Foo<K, V> { map: HashMap<K, V>, } impl<K, V> Foo<K, V> { fn insert_something(&mut self, k: K, v: V) { self.map.insert(k, v); } } The compiler errors are: error[E0599]: no method named `insert` found for struct `std::collections::HashMap<K, V>` in the

How to add constraints on generics

﹥>﹥吖頭↗ 提交于 2020-12-04 00:41:58
问题 I'm having trouble finding out how I constrain the generic types. It seem like K need to implement the core::cmp::Eq and core::hash::Hash traits. I've been unable to find the required syntax in the docs. use std::collections::HashMap; struct Foo<K, V> { map: HashMap<K, V>, } impl<K, V> Foo<K, V> { fn insert_something(&mut self, k: K, v: V) { self.map.insert(k, v); } } The compiler errors are: error[E0599]: no method named `insert` found for struct `std::collections::HashMap<K, V>` in the

How to add constraints on generics

限于喜欢 提交于 2020-12-04 00:38:20
问题 I'm having trouble finding out how I constrain the generic types. It seem like K need to implement the core::cmp::Eq and core::hash::Hash traits. I've been unable to find the required syntax in the docs. use std::collections::HashMap; struct Foo<K, V> { map: HashMap<K, V>, } impl<K, V> Foo<K, V> { fn insert_something(&mut self, k: K, v: V) { self.map.insert(k, v); } } The compiler errors are: error[E0599]: no method named `insert` found for struct `std::collections::HashMap<K, V>` in the

How to add constraints on generics

[亡魂溺海] 提交于 2020-12-04 00:38:17
问题 I'm having trouble finding out how I constrain the generic types. It seem like K need to implement the core::cmp::Eq and core::hash::Hash traits. I've been unable to find the required syntax in the docs. use std::collections::HashMap; struct Foo<K, V> { map: HashMap<K, V>, } impl<K, V> Foo<K, V> { fn insert_something(&mut self, k: K, v: V) { self.map.insert(k, v); } } The compiler errors are: error[E0599]: no method named `insert` found for struct `std::collections::HashMap<K, V>` in the

How to perform a checked cast?

若如初见. 提交于 2020-12-02 08:21:50
问题 I am new to Generics and am having an issue. Consider the following code: public class A {} public class B extends A {} public <T extends A> T getB() { A test = new B(); Class<B> clazz = B.class; if (clazz.isInstance(test)) { return (T)test; } return null; } This generates an Unchecked cast warning. on the return (T)test; line. but clearly I am checking the type with the if (clazz.isInstance(test)) line. Is there a way to do a "checked cast"? I'm not looking to just suppress the warning but

Issue with generic properties when type mapping

北战南征 提交于 2020-12-02 06:16:57
问题 I have a library which exports a utility type similar to the following: type Action<Model extends object> = (data: State<Model>) => State<Model>; This utility type allows you to declare a function that will perform as an "action". It receives a generic argument being the Model that the action will operate against. The data argument of the "action" is then typed with another utility type that I export; type State<Model extends object> = Omit<Model, KeysOfType<Model, Action<any>>>; The State

Issue with generic properties when type mapping

家住魔仙堡 提交于 2020-12-02 06:16:10
问题 I have a library which exports a utility type similar to the following: type Action<Model extends object> = (data: State<Model>) => State<Model>; This utility type allows you to declare a function that will perform as an "action". It receives a generic argument being the Model that the action will operate against. The data argument of the "action" is then typed with another utility type that I export; type State<Model extends object> = Omit<Model, KeysOfType<Model, Action<any>>>; The State

Issue with generic properties when type mapping

此生再无相见时 提交于 2020-12-02 06:14:30
问题 I have a library which exports a utility type similar to the following: type Action<Model extends object> = (data: State<Model>) => State<Model>; This utility type allows you to declare a function that will perform as an "action". It receives a generic argument being the Model that the action will operate against. The data argument of the "action" is then typed with another utility type that I export; type State<Model extends object> = Omit<Model, KeysOfType<Model, Action<any>>>; The State

Issue with generic properties when type mapping

时光毁灭记忆、已成空白 提交于 2020-12-02 06:14:21
问题 I have a library which exports a utility type similar to the following: type Action<Model extends object> = (data: State<Model>) => State<Model>; This utility type allows you to declare a function that will perform as an "action". It receives a generic argument being the Model that the action will operate against. The data argument of the "action" is then typed with another utility type that I export; type State<Model extends object> = Omit<Model, KeysOfType<Model, Action<any>>>; The State