Questions tagged [type-inference]
Type inference is the process of inferring types for programs automatically, using rules defined by a type system.
2,406
questions
0
votes
0
answers
37
views
how could inference in typescript help me? [closed]
I have a code with physical quantity's in it and they have multiple units, for example length has m , cm ,ft. what I want to do is to use inference so that for example when I compile the code for ==&...
3
votes
2
answers
34
views
Type inference changing from unknown to known type after using method
I have a class with single generic T, where sometimes it may be constructed with a value so T is known, sometimes constructed without a value and so T is unknown. It is tedious to have to specify the ...
1
vote
1
answer
72
views
Infer a type from a string in Haskell
I'm working on a JSON data encoder in Haskell! (to be more specific, I am trying to port Jo into Haskell!). I've gotten a lot of it working, but I'm running into a little wrinkle. I'll try to be ...
1
vote
1
answer
39
views
How does type deduction for builtin method of integer types work in rust?
I'm quite confused about how Rust infers the exact type of an integer. Type deduction seems to work differently for builtin methods than for traits implemented for multiple integers.
For example:
...
0
votes
0
answers
13
views
Protege; infer that if two apartments are located in the same address, then they are in the same building
I am pretty new at building ontologies in Protege. I am doing a simplified real estate ontology. I have an object property called "isLocatedIn" (actually my ontology is in spanish, so that ...
0
votes
1
answer
27
views
Function composition type inference in Dart
In dart, with generics,
the identity function works as below:
a to a:
var id = <A>(A a) => a;
a to the list type [a]:
var list = <A>(A a) => [a];
Now, I have function composition ...
0
votes
0
answers
30
views
Infer conditional return type from generic object properties parameter of function for DataLoader
I was working recently on typesafe reconciliation of GraphQL DataLoader. Basically, I am creating a function that will order a collection of elements to match the order of keys (check constraints on ...
0
votes
0
answers
22
views
Typescript and React, different Types between Compile Time and Runtime for React.KeyboardEvent<HTMLInputElement>
TL;DR Object.getPrototypeOf(e.target) returns HTMLInputElement but the compiler says that the type is EventTarget
Long:
I have a simple input
<input
className="FilterInput"
...
0
votes
2
answers
43
views
Why rust fails type inference for Vec<_>
I don't understand why Rust fails to infer the type for Vec<_> in some cases. If I run this
fn main() {
let v = vec![1, 2, 3];
let s = v.iter().sum();
}
I get an error:
error[E0282]: ...
0
votes
1
answer
35
views
Generic TypeScript type for an array of successive mapping functions
I am mapping an object of type A multiple times via a succession of functions.
For example:
first a function with type (item: A) => B
then a function with type (item: B) => C
then a function ...
1
vote
1
answer
42
views
Allow only keys from type of the object in an array
export const groupBy = <T, K extends keyof T>(key: K, array: T[]) =>
array.reduce((objectsByKeyValue, obj) => {
const value = obj[key];
objectsByKeyValue[value] = (...
1
vote
0
answers
36
views
Typescript `Array.join` literal return type
I have an array like:
const arr = ['a', 'b'] as const;
// type: readonly ["a", "b"]
Now I want to join the strings in the array:
const joined = arr.join('');
// type: string
My ...
1
vote
1
answer
21
views
How to narrow object type based on field presence?
I have 2 similar endpoints, that's one for getting a list, the other for getting a filtered list. Now it's not possible to merge them into one endpoint with query parameters on backend. So I have a ...
2
votes
1
answer
55
views
How can I manually calculate the type of functions with class constraints?
The following typing rule for function application
f :: A -> B
e :: A
-----------
f e :: B
doesn't take into account an f with class constraints. How can I manually calculate the ...
0
votes
1
answer
34
views
TS infer parameters for each function member of an array
I'm trying to create a function utility where I can pass in an array as the argument to the function, where each member is an array with the following structure:
[ function, functionParams ]
...
0
votes
0
answers
27
views
Infer generic TValue from generic constraint of T
I'm trying to make a method that should infer a generic type from a constraint of another type argument.
I could try to do some runtime type-checking, but it would be nice to have absolute compiler ...
0
votes
1
answer
30
views
how to correctly type the keys of an object in typescript
I want to traverse the properties of an object and have Typescript correcly infer the type of the key, but I can't seem to achieve, and I have to cast it.
Here's an example
type Cycle = 'year' | '...
2
votes
0
answers
50
views
Python input_schema gives 'list index out of range' when using classes
I'm using the inference-schema package in order to convert data from an API call to a correctly formatted Pandas dataframe.
This worked great until i tried to put the Run method inside a class, then ...
-3
votes
3
answers
105
views
How to change a variable type in Java?
I have two objects:
Employee E
PTEmployee PE
I need to do the following manipulation
EV=E
EV=PE
I believe I would need to do the following:
var EV = E
But then when I set EV = PE, I cannot access ...
0
votes
1
answer
108
views
How to infer default type in Typescript
I'm trying to provide a default type for a generic type but it should infer the class extending the interface and not the interface:
export interface Example {
something: (...p: any[]) => any;
}...
1
vote
1
answer
122
views
How can I avoid explicit recursion in this case?
I wound up with this skeleton:
f :: (Monad m) => b -> m ()
f x = traverse_ (f . g x) =<< h x -- how avoid explicit recursion?
g :: b -> a -> b
-- h :: (Foldable t) => b -> m (t ...
1
vote
0
answers
47
views
Why does parameter type inference break in compose function?
I encountered this in my own library, so I thought I would check against the Redux compose function that does the same thing. I found the same issue.
Because redux is considerably more popular than my ...
3
votes
1
answer
52
views
Is it possible to infer generic type from inside string template literal
The following piece of code does exactly what I intend to know with one caveat, I would like to avoid having to explicit the generic it needs.
type EventGroup = {
actions: "run" | "...
0
votes
1
answer
16
views
Export Redux store state type when configuring it asynchronously
everyone!
I'm trying to configure Redux store with preloaded state, which I can obtain only asynchronously using TypeScript.
Question
Am I able to lazy infer state and dispatch types like it's ...
1
vote
1
answer
27
views
Advanced Typescript inference
I am currently trying to build a plugin library. These plugins have some methods in common, and others that are really different.
Example :
class PluginA extends Plugin {
specificAMethod() {
.......
1
vote
0
answers
35
views
Using the TypeScript compiler API how can I get the actual, inferred type parameters for a call?
I would like to use the TypeScript compiler API to get information about inferred types.
For example, in
// Two generic functions
function identity<T>(x: T): T { return x }
function copy<U>...
1
vote
0
answers
25
views
How can I extract map keys from generic argument that can be a Record OR function that returns Record? [duplicate]
I have a function, that takes an argument.
The argument can be one of:
a Record<key?, SomeType>
a (arg: AnotherType) => Record<key?, SomeType>
How can I make the function's return ...
1
vote
1
answer
42
views
Not explicitly returning in callback of method chain has compiler incorrectly infer current subject
In the first example, myString has ts 2322 error because it believes returnedString is a number (it is not). In the second example, if we explicitly return getNumber(), there is no ts 2322 error.
...
0
votes
1
answer
25
views
How to infer deep object?
Let's say I have an object that looks like this
const myObj = {
homePage: ["on", {
title: 'Home Page',
}]
})
I want to restrict object values but infer keys. I saw another ...
4
votes
1
answer
97
views
How to get rid of these apparently superfluous `undefined`s?
I'm using GHC 9.2.2 and playing with OverloadedRecordDot and generic-lens. As an experiment, I want to use the overloaded dot as a "frontend" to the generic-lens functionality (including ...
0
votes
0
answers
43
views
How can treat string as string literal when mapping object in typescript
Here is my code
I tried my best to narrow down the type, the Name is still string
type Item = { readonly name: string }
const foo: Item = { name: 'foo' } as const
const bar: Item = { name: ...
1
vote
0
answers
47
views
Javac 17 type inference on ternary operator
While migrating from Java 11 to Java 17, I've stumbled on a compilation error on a method that looks like this (using javac):
public List<String> getList() {
return true ? Collections....
6
votes
3
answers
131
views
Invariant Generics don't seem working correctly
I've read some articles about Covariance, Contravariance, and Invariance in Java, but I'm confused about them.
I'm using Java 11, and I have a class hierarchy A => B => C (means that C is a ...
0
votes
1
answer
91
views
Typescript extract exact Discriminated unions type in iterator method
export type FILTER_META =
| {
type: 'string';
key: string;
filters: { id: string; label?: string }[];
}
| {
type: 'time';
key: string;
filters: { min: ...
1
vote
1
answer
41
views
infer values from objects using object keys
Been looking for an answer for this, but couldn't find an exact one.
I have to following code example:
interface RowData {
firstName: string;
lastName: string;
age: number;
participate:...
0
votes
0
answers
43
views
typescript infer object value from key
I have a map of components like this:
import { Select, Input, DatePicker } from 'antd';
const MyComponentMap = {
Select,
Input,
DatePicker
}
I'm trying to create a JSON structure like this:
...
1
vote
1
answer
48
views
Why doesn't rustc infer the type based on std trait implementations?
Consider the following code:
use std::net::IpAddr;
pub struct Server {
host: IpAddr
}
impl Server {
fn new(host: IpAddr) -> Self {
Self {host}
}
}
fn main() {
let host = &...
1
vote
1
answer
87
views
Is it possible to infer type parameters from what return values are assigned to?
Suppose I wrote two functions like this:
func ToInterfaceSlice[T any](s []T) []interface{} {
res := make([]interface{}, len(s))
for i, v := range s {
res[i] = v
}
return res
}
...
1
vote
2
answers
25
views
Trouble understanding type indexing
I'm having trouble understanding the following type definition:
type MergeHandlersReturnType<H extends Record<string, any>> = {
[K in keyof H]: ReturnType<H[K]>
}[keyof H]
...
1
vote
1
answer
38
views
How to get type inference using a utility type, or type parameter, when I extend the built-in array class?
Defining types for Class-member's in an Extended Array Class
So below, you can see that I extend the array class via the WillyWonka class. The problem is, when I do it as shown below, I get no type ...
0
votes
0
answers
29
views
How to infer object with generic items
I have an object like this and infer types using settingToType helper:
enum SettingType {
Departures = 'departures',
Dashboards = 'bookings_dashboards',
MainNumber = '...
2
votes
1
answer
31
views
dart lambda type inference
void main() {
print(doStuff.runtimeType);
print(((e) => doStuff(e)).runtimeType);
}
int doStuff(String hallo) {
return 42;
}
executed in the dartpad yields
(String) => int
(dynamic) =>...
1
vote
1
answer
40
views
Unable to infer property type even though it's extending from a record definition
I'm confused why typescript is unable to infer that destination[destinationProperty] is a TChild when TParent extends Record<TParentProperty, TChild> which should allow it to infer that the ...
0
votes
0
answers
13
views
how can estimate Garch model Residual?
I have two questions about the garch model and I need your help. Please help me if possible. I need to answer these questions
Does the "infer" command in MATLAB estimate the variance for ...
1
vote
1
answer
49
views
Typescript is not properly inferring types from intermediately defined types
I have the following type definitions.
type Triple<A, B, C> = A & B & C;
type First<T> = T extends Triple<infer A, infer _B, infer _C> ? A : never;
type Second<T> = T ...
2
votes
1
answer
57
views
Bad type inference for the Vec of dynamic closures
I am trying to get a list of partially applied functions, like so:
fn partially_applied() -> Vec<Box<dyn Fn(u32) -> u32>> {
let xs = vec![1_u32, 2, 3];
xs.into_iter().map(|x|...
0
votes
1
answer
87
views
Type for TypeORM Raw Entity: CamelToSnake / ValueOf Exact Matching / Property-Method inference
Summary: How can I generate type for Raw Return types in TypeORM ?
Hello, on TypeORM getRawOne and getRawMany function returns string which has class name and properties that joined with underbar('_')
...
0
votes
0
answers
20
views
why Dart type inference not work in this case? [duplicate]
/// the widget
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
class AsyncValueBuilder<T> extends StatelessWidget {
const ...
1
vote
2
answers
61
views
TypeScript build union type from complex object
I have the following object:
const object = {
root: {
path: 'path1',
},
paths: [
{
path: 'path2',
},
{
path: 'path3',
}
],
};
and I would like to build a union ...
0
votes
0
answers
19
views
Why does adding an explicit type parameter to `Source.actorRef` cause "missing parameter type for expanded function" error?
This code, adapted from a Source.actorRef documentation, works fine:
import akka.actor.ActorSystem
import akka._
import akka.actor.ActorRef
import akka.stream.scaladsl._
implicit val system = ...