type-inference

Aux-pattern usage compiles without inferring an appropriate type

人盡茶涼 提交于 2021-02-04 19:20:27
问题 Consider the following simple example involving Aux -pattern: sealed trait AdtBase abstract case class Foo(){ type T <: AdtBase } object Foo{ type Aux[TT] = Foo { type T = TT } } abstract case class Bar(){ type T <: AdtBase val foo: Foo.Aux[T] } object Bar { type Aux[TT] = Bar { type T = TT } def apply[TT <: AdtBase](f: Foo.Aux[TT]): Bar = new Bar() { override type T = TT override val foo: Foo.Aux[T] = f } } case class Baz(foo: Foo) def testBaz(baz: Baz) = Bar(baz.foo) //Compiles fine def

Typing an array of generic inferred types

情到浓时终转凉″ 提交于 2021-02-04 18:52:06
问题 I'm trying to create the type of an array of objects. The first and second key of this object is required to match. For example: [{ key1: "hi", key2: "world" },{ key1: 1, key2: 2 },{ key1: true, key2: false }] This is what I've come up with but it doesn't exactly work. I have a generic type to define the object in the array. When calling it to generate the array type, an error is raised. type ArrayItem<T> = { key1: T, key2: T } // This raises an error Generic Type ArrayItem requires 1 type

How to infer inner type of Shapeless record value with unary type constructor?

孤人 提交于 2021-02-04 12:36:52
问题 I'm having trouble understanding the way the Shapeless record Selector interacts with scala's type inference. I'm trying to create a method that can grab a field from a Shapeless record by key, only if the value of the field has a certain unary type constructor, in this particular case Vector[_] , and then grab an inner value of inferred type V out of the Vector , in this case with Vector.apply() . I feel like I'm close. This works, with a concrete inner type of Int : val record = ( "a" ->>

typescript inference without enumerating all possibilities

早过忘川 提交于 2021-02-04 08:14:07
问题 I would like to know if there is a solution for the following problem : I have two functions that accept different input parameters, I have an object mapping each function to a string. I define a type that is a union type of object containing the key of one function in the map and a value matching the function parameters type. Given an object with that type, I would like to retrieve the corresponding function from the map object and call it with the value but typescript does not do the type

typescript inference without enumerating all possibilities

给你一囗甜甜゛ 提交于 2021-02-04 08:12:08
问题 I would like to know if there is a solution for the following problem : I have two functions that accept different input parameters, I have an object mapping each function to a string. I define a type that is a union type of object containing the key of one function in the map and a value matching the function parameters type. Given an object with that type, I would like to retrieve the corresponding function from the map object and call it with the value but typescript does not do the type

Inferring nested value types with consideration for intermediate optional keys

痞子三分冷 提交于 2021-02-02 03:37:42
问题 I'm trying to define helper types for determining the type of nested object values, whilst also considering any optional parent keys, e.g. in structures like these (or deeper): type Foo = { a: { b?: number; } }; type Foo2 = { a?: { b: number } }; For my purposes, the type of b in both Foo and Foo2 should be inferred as number | undefined . In Foo2 the b is not optional itself, but because a is, for my lookup purposes b must now be optional too... so much for context. Using these helper types

Why can't the compiler select the correct String.contains method when using this lambda shorthand?

牧云@^-^@ 提交于 2021-01-29 14:30:00
问题 Say I want to check if a string contains any of the letters in "cory": def hasCory(input: String): Boolean = { val myName = "cory" input.exists(myName.contains) } The compiler complains with: error: type mismatch; found : CharSequence => Boolean required: Char => Boolean Scala provides the Char -accepting method I want in StringOps: But it appears that the compiler cannot see this method unless I change the code to one of: input.exists(myName.contains(_)) input.exists(c => myName.contains(c))

Use function interface to ensure parameters but infer more specific return type

可紊 提交于 2021-01-29 06:13:13
问题 I have a function signature that I need a bunch of functions to follow which is something like this: type ActionCallback<R = any> = (param1: SpecificType, param2: OtherType) => Promise<R> Basically the types for the parameters are well defined and it must return a promise but what that promise resolves to is up to the function. Instead of having to specify the type of both arguments in every callback I'd like to just specify the variable conforms to ActionCallback so the parameters types are

Typescript inference issue - Return type of exported function has or is using name … but cannot be named

孤人 提交于 2021-01-28 10:53:42
问题 I want to create a function which transform a Promise based function, in my example from mongodb node client , into an Observable. The code is very simple export function updateOneObs(filter: Object, object: Object, collection: Collection<any>) { return Observable.fromPromise(collection.updateOne(filter, object)); } If I put this in VSCode, I get an error message that says "Return type of exported function has or is using name 'UpdateWriteOpResult' from external module ".../npm-packages

Is there a way to declare a union type in TypeScript with computed strings based on existing constants?

匆匆过客 提交于 2021-01-28 04:23:56
问题 Let's assume we have following constant: const something = { foo: { bar: { num: 67, str: 'str', }, }, some: { prop: 12, }, topProp: 25, }; The Task: Implement typechecking for following deep property access function /** * @example * getByPath('foo/bar/str'); // returns 'str' * * @example * getByPath('topProp'); // returns 25 * * @example * getByPath('some/prop'); // returns 12 */ const getByPath = (path: ComputedUnionType) => {<unrelated-code-magic>}; // Where type ComputedUnionType = 'foo