Questions tagged [type-inference]
Type inference is the process of inferring types for programs automatically, using rules defined by a type system.
type-inference
2,717
questions
0
votes
2
answers
103
views
C# Generic Type Argument Inference for Method Parameters: Workarounds?
I'm working on a generic method that accepts parameters whose types should be inferred from the generic type arguments of a given interface. I'd like to achieve compile-time type safety similar to the ...
1
vote
1
answer
36
views
why can't typescript figure out that the possible actual types of a union type correspond to the available prototypes for a function? Workaround?
This (playground link) doesn't work:
type ReplaceAll2ndArgType = string | ((substring: string, ...args: unknown[]) => string)
export async function renderHTMLTemplate(
args: Record<string, ...
1
vote
1
answer
33
views
In TypeScript why does [1, 2] extend [...unknown[], 0, ...unknown[]]?
I'm trying to understand how infer in TypeScript conditional types work. One thing I've noticed about infer types in TypeScript is that they are defined to allow a rest element following another rest ...
1
vote
1
answer
41
views
Typesafe nested function return using generics
I have a the following mapper with the type associated with, but I have no clue what to look, to type func correctly.
type Action<T, K> = {
key: K;
func: // What should this be?
};
type ...
2
votes
1
answer
40
views
Typescript infers type never on Union of two types
In the following example, in the getAdditionalData function, Typescript infers the type of value to never after validating the type through isBarType. I would expect the type to be FooEntity since ...
1
vote
1
answer
69
views
Looking for a c++20 typeof() equivalent [closed]
In a template function I would like to declare a vector (or set...) where T is the type of the data contained in another container.
Tried this under VisualStudio 17.9.7:
#include <iostream>
#...
2
votes
1
answer
39
views
Validate different types in the same array in TypeScript
How MyType should be defined to ensure that each item of my array is an array composed of item of the same type?
const array1: MyType = [["foo", "bar"], [42, 42], [true, false]]; //...
1
vote
1
answer
38
views
How to ensure object values are keys of a type deduced by the object key with TypeScript?
Given
// GIVEN
type Animal<T extends string> = {
id: T,
}
type Dog = Animal<"animal.dog"> & {
foo: string
}
type Cat = Animal<"animal.cat"> & {
...
0
votes
0
answers
46
views
Restrictions on auto keyword for type inference in C2X
int main()
{
auto status = 203;
auto static const str = "Hello";
auto static const strs = {"Hello", "World"};
return status;
}
From playing around with ...
0
votes
0
answers
21
views
How to let typescript infers type of `this` in nested scope? [duplicate]
I use typescript v5.4.5 (playground) and have an issue. There is my code example
function wrapper<C>(ctx: any, fn: (r: any) => void) {
const r = {};
return fn.call(ctx, r);
}
class ...
1
vote
2
answers
46
views
How can I accurately determine the type in this particular scenario?
Consider the function f, which is defined as follows:
function f<T extends Fields = Fields>(props: Props<T>) {
return null;
}
In this function, T represents a generic type that extends ...
1
vote
0
answers
15
views
Typescript infer generics with callback argument [duplicate]
I'm having a hard time inferring TypeScript generic arguments in a function. The idea is that I just specify one generic argument T and the generic U is inferred from a callback, like so:
async ...
2
votes
1
answer
54
views
Inferring a type
I'm revisiting an issue after a year of being stumped by it. I have no idea how to frame this question concisely so please bare with me.
ISSUE: I want to narrow the union type of a nested object by ...
1
vote
0
answers
24
views
How to solve: Type mismatch: inferred type is (PuQuPrintManager) -> Unit but () -> Unit was expected
Testing Bluetooth connection with mobile printer.
I am getting "Type mismatch: inferred type is (PuQuPrintManager) -> Unit but () -> Unit was expected"
compiler error.
I am lost in ...
1
vote
1
answer
49
views
Why is there no type-error when assigning a conditional return-type of string to a number?
I want to define a typescript function with a conditional return-type that depends on the type of the argument: Called with an argument of type true it returns a string and called with an argument of ...