Questions tagged [strong-typing]
A typing system that enforces restrictions on which operations or methods may be called on an object based on type.
447
questions
1
vote
2
answers
47
views
How to patch form array in typed angular reactive forms
I am trying to figure out how to patch form array in a typed reactive Angular form. patchValue and setValue does not work in my mind consistently with FormControl for example.
Here is a form
this....
2
votes
0
answers
115
views
Typescript: How to satisfy union type with function and arguments
The union of (arg: string) => void and (arg: number) => void is (arg: never) => void, because no value can be a string and a number at the same time. But how can I work around this issue in ...
0
votes
1
answer
48
views
TypeScript: How to initilize keyof T (generic) at runtime
so I have this TypeScript class where I have a generic and a property of type keyof generic, like this:
export class ColumnDefinition<T> {
field: keyof T
}
Compiler complains because it ...
0
votes
0
answers
17
views
Unable to load Serilog.Enrichers.CorrelationId, Version=3.0.1.0, Culture=neutral, PublicKeyToken=null since it is not strongly typed
I am trying to load Serilog from an assembly which is strongly typed and is in GAC. I am unable to load assembly Serilog.Enrichers.CorrelationId, Version=3.0.1.0, Culture=neutral, PublicKeyToken=null, ...
0
votes
1
answer
22
views
How to define static variables in TypeScript [duplicate]
I want to define an array with a constant value
e.g. const arr = ["a", "b"];. However, the compiler will think it's string[]. Is there is a way to make it a constant type as is so ...
3
votes
2
answers
80
views
TypeScript method to wrap any method of another class
I have the following setup in TypeScript:
abstract class Test {
public abstract method1(param1: string): number;
public abstract method2(param1: number, param2: string): Promise<number>;
...
0
votes
2
answers
66
views
Raising compile-time errors in order to constraint possible permutations of a struct in Rust
Imagine a struct...
struct Pair {
letter: Letter,
number: Number,
}
enum Letter {A, B, C}
enum Number {One, Two, Three}
The total number of combinations in this struct would be 3 x 3 = 9, ...
1
vote
1
answer
85
views
Type Challenge 11 Tuple to Object
I am fairly new to strong typing but have been working in TypeScript for a little while now. I am starting to work through the Type Challenges to get better at strong typing and having a strong ...
1
vote
1
answer
1k
views
Get Literal types for object keys in typescript dynamically?
I want to get object keys type, here i write a generic for object with keys as string
type GenericInput = {
[key:string]: {value:string,type:HTMLInputTypeAttribute,placeholder:string,min?:number,max?...
1
vote
1
answer
43
views
Determining the type of a value based on a if check on another value
I want my code editor to infer the type of extraData based on the value of error which is being narrowed by the if statement:
export enum ErrorCodes {
Unknown = 'UNKWN',
BadRequest = 'BDREQ',
}
...
0
votes
1
answer
54
views
Using a subset of an object in different services
lets say that I have a system that heavily relies on a single object, for example a Person.
I have a storage service (service 1) that handles basic CRUD operations for Person, and saves this person in ...
0
votes
2
answers
514
views
io-ts to strict validate if object has no declared property
I'm using io-ts to do input validation for an API. I need to ensure there has no un-declared property in the requirement object.
For example, if I define the interface of request as following:
const ...
3
votes
1
answer
600
views
Conditional types with mypy
I have the following code snippet:
from typing import TypedDict
class Super(TypedDict):
foo: int
class SubA(Super):
bar: int
class SubB(Super):
zap: int
def print_props(inp: Super, key:...
0
votes
1
answer
375
views
How to get type hint in php array similar to java or typescript?
I read various articles claiming that now php-8 also is strongly type language
https://medium.com/codex/php-became-strongly-typed-66f2b2ae917. Since I work with typescript, i was looking for same IDE ...
1
vote
1
answer
360
views
How to achieve strongly typed integers in Python
I don't think that I like that the following is True. I understand that's the case because NewType is only examined by the static checker, and ignored at runtime. If I want it to be False, what's ...
6
votes
0
answers
121
views
Comfortable way to use strong types instead of bools for parameter flags?
Suppose you have a function that takes a boolean argument:
void do_something(bool use_some_option);
This has issues because on the call site, you see something like do_something(false) which is not ...
1
vote
1
answer
2k
views
How to have TypeScript generic function with type argument being keyof an object have return type of that object's key's value
I have a several object types:
type Slave = {
myKey:string
}
type AnotherSlave = {
anotherKey:string
}
And a master type that contains some keys, and these object types as those keys' values as ...
0
votes
1
answer
701
views
Method pow does not exist error Pyspark error
This is a bit strange.
When I create a dataframe and then do some transformations with the function pow, it works. But when I push it to run in a real world scenario, it does not. The datatype of ...
0
votes
1
answer
186
views
Describing complex return type of the method
I'm trying to define return type of the method in Python (3.8.9).
Ideally, return type should be list of tuple of int. My first natural solution, as I came from strongly typed world, was def .... ->...
1
vote
1
answer
1k
views
Why does TypeScript's Omit not enforce the value of the omitted properties?
Background: (can be skipped)
I was fiddling around to make some update(person: Person) statement more usable by allowing a subset of properties it should update. I was thinking there are 2 options:
...
0
votes
1
answer
3k
views
"Bad return type in lambda expression: String cannot be converted to void" in .forEach method
I have a class Users with, among others, a List of user objects, the following method, which is supposed to
build a list of String
check if an incoming String is contained in that list.
public class ...
0
votes
0
answers
77
views
Why sometime I need to explicitly specific the generic type when calling constructor/function in C#?
Refer to the image above, we can see the AbpAsyncDeterminiationInterceptor must have a generic type which is the same as what is provided into its constructor. Supposed it should be interpreted ...
5
votes
2
answers
2k
views
Is there a way in TypeScript to say "Include" (opposite of Exclude)?
I have a String Union that looks like:
type AlphabetLike = 'a' | 'b' | 'c' | 'zeta' | 'beta' | 'gamma' | 'mu';
I want to be able to construct the type
type Alphabet = 'a' | 'b' | 'c'. I could do this ...
0
votes
1
answer
357
views
TS2339: Property 'data' does not exist
I have this angular store configuration
action.ts
export const saveUserAction = createAction(
'[User] Save',
props<{ payload: User }>
);
export const saveUserSuccessAction = ...
31
votes
7
answers
14k
views
Angular 14 strictly typed reactive forms - How to type FormGroup model using existing interface
I'm just playing around with new angular typed forms api and couldn't figure out how to type FormGroup without declaring specific "FormInterface" witch has to match the original one.
Maybe I'...
1
vote
1
answer
212
views
Omitting keys of a particular type
Here is what I would like:
const a = getNumber({a: 3, b: "three"}, "a"); // a is type of number (or even 3)
const b = getNumber({a: 3, b: "three"}, "b"); // ...
0
votes
1
answer
592
views
Defining a method that is strongly typed and allows only inherited types
I want to define a method for a class Dev that must be strongly typed and allows only types
inherited from the class Dev that uses a constructor with no parameter.
The following is what I came up with:...
0
votes
0
answers
111
views
Strongly Typed IDs and JSON Schema generation in C#
I like the idea of Strongly Typed IDs and I am using it for some time. I use it also in DTOs without and major problems... till now. I have a use case where I need to generate JSON Schema from some ...
1
vote
2
answers
61
views
Java: casting primitive wrappers classes
I have following method that I use when traversing values in JSON document:
protected static <T> T getValueAs(Object untypedValue, Class<T> expectedType) {
if (expectedType....
1
vote
2
answers
133
views
Using generics, how can I create a tuple of objects from a captured array?
Given a function like this:
function makeObjects<T extends string[]>(...values: T);
Make the return value this:
T.map(v => ({ [v]: any }));
I'm using an array map to show what it looks like ...
2
votes
1
answer
637
views
Abstraction - How do I create a generic static method for several types to flow through in Dart/Flutter?
Okay, I'm trying to cut back on an enormous amount of boilerplate code for my app. Right now I see the need for some refactoring and serious abstraction.
I want to abstract CRUD actions for my ...
1
vote
1
answer
423
views
PHP: Declaring a "numeric" type
With modern versions of PHP, we can declare many different types, union types or even mixed. However, I work with Magento 2, and its often impossible to know if you're going to get an ID of 7 or '7' ...
1
vote
1
answer
361
views
Strong types in Protocol Buffers
Since protobuf has no support for type alias is it a valid approach to create a strong type using a message with a single value member like the following?
message ID {
uint32 value = 1;
}
message ...
1
vote
2
answers
93
views
kotlin get static type of a class property
I'm trying to get the type of some class properties in order to strongly typing my Kotlin Code.
In typescript, we can do this (stupid examplebut this is to explain)
class Test {
private _prop:...
2
votes
1
answer
888
views
Accessing TypeScript Optional Object Properties With a String
I'm working in an existing JavaScript codebase. There is a class which exposes pre-defined functions (e.g. "copy", "paste") for utility. The class can be instantiated with "...
1
vote
0
answers
33
views
Usage of NamedType and type_safe implementations
Recently, there was a talk in my team to use strong types. After a bit of reading different blogs, I am inclined towards avoiding Primitive Obsession and have found below 2 implementations.
NamedType ...
0
votes
1
answer
395
views
Typing does not work when creating a map in Kotlin through spring boot @RequestParam
I am using spring boot and have made the following controller where I explicitly specify key and value types.
@PostMapping("DAC/set")
fun setDac(@RequestParam map: HashMap<Int, Float&...
1
vote
0
answers
984
views
Make Dictionary<Type, Dictionary<string, object>> strongly-typed
In my application users can search for different things in various places. The same search code could be used for different things. I'm trying to create a cache in order to avoid querying the database ...
1
vote
0
answers
383
views
C# Use an undefined number of generic types in the signature of the generic class definition
I was checking in the c# 5,6 specs and is not clear if it is possible use a undefined number of types, such as an array, in the signature of a Generic class definition. As it is very know yu can use ...
1
vote
0
answers
263
views
Mypy type narrowing from return values
I'm looking at the following typed Python example:
from __future__ import annotations
from typing import Optional
class Thing:
def does_intersect(self, other: Thing) -> bool:
return ...
1
vote
2
answers
2k
views
Is it "Pythonic" to type check function arguments? [closed]
Coming from C/C++ background I have always been little set back by the Python's lack of strong type checking. Therefore it always led me to writing code of this style:
def function (...
2
votes
1
answer
111
views
TypeScript Wrapper Function Inferring Types
I use a little utility/wrapper function for functions related to database operations. It should take a specific function fn with the type Promise<PromiseReturnType<GENERIC>>, then do some ...
2
votes
1
answer
194
views
Exposing Strongly Typed Ids for Application Commands?
I am using strongly typed ids in my domain model, mostly following the guidance from Andrew Lock at:
https://andrewlock.net/using-strongly-typed-entity-ids-to-avoid-primitive-obsession-part-1/
These ...
0
votes
1
answer
663
views
How do I emit a signal argument as an int instead of string?
In Godot 3.3, I'm trying to make a Label respond to text entered through a LineEdit node. I connected the objects and can emit the signal, but the signal is only ever sent as a string, not as the int ...
1
vote
0
answers
148
views
Strongly-typed reference from input tag-helper
Context
Here's an instance of a form tag helper from one of the ASP.NET Core documentation example projects:
<form asp-page="./Index" method="get">
<div class="...
0
votes
1
answer
1k
views
How to create a `Typed` array in php? [duplicate]
I'd like to do something like that, creating a defined type array of classA inside classB, However, I can't find the syntax.
class classA{
}
class classB{
private classA $someName:array;
}
0
votes
1
answer
659
views
Angular/Typescript: declare return type of lambda passed to "find"
So, I am following a training course and have an Angular service:
import {Exercise} from "./exercise.model";
export class TrainingService{
private availableExercises: Exercise[] = [
{ ...
1
vote
0
answers
171
views
Is there a predefined DOM Event object for scroll event?
I'm creating an event handler for a component in Angular.
The problem I'm having is right now is: I can't find a typed event for scroll, therefore in the handler's argument I must use any.
I see there ...
1
vote
1
answer
861
views
Typescript: Strong-typing (and autocomplete) for a value based on a sibling object's keys
Say I have an object that looks like this:
const configuration: Config = {
options: {
'Option 1': 'some value here',
'Option 2': 'some other value here'
},
defaultOption: 'Option 1'
}
...
2
votes
1
answer
512
views
Why does annotating this object with a Record type remove Intellisense?
I am creating a data-file that looks like this:
interface ISprite {
textureName: string,
frame: Frame,
origin: Vec2,
zIndex?: number
}
export let sprites: Record<string, ISprite> = {
...