Questions tagged [strong-typing]
A typing system that enforces restrictions on which operations or methods may be called on an object based on type.
422
questions
1
vote
2
answers
43
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
32
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 ...
0
votes
1
answer
49
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 boiler plate 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
votes
1
answer
71
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' ...
0
votes
1
answer
22
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
34
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:...
1
vote
1
answer
88
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 "...
0
votes
0
answers
9
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
33
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&...
0
votes
0
answers
62
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
69
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 ...
0
votes
0
answers
100
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 ...
0
votes
0
answers
17
views
Static vs dynamic, compiled vs interpreted... what do you call these language categories?
I was thinking of how to map a programming language as an entity when I encountered a problem trying to assign an attribute name for these major categories that differentiate programming languages.
...
1
vote
2
answers
198
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 (...
0
votes
0
answers
33
views
Extract return Observable<type> from complex array parameter of mixed types, some missing data
I have a function that creates an observable from an array of data, with some rules as to exactly what is emitted. I'd like to strictly type that observable, narrowing down the type of emitted values ...
2
votes
1
answer
69
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 ...
1
vote
1
answer
39
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
80
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
42
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
103
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
99
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[] = [
{ ...
0
votes
0
answers
62
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
88
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'
}
...
1
vote
1
answer
47
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> = {
...
0
votes
1
answer
805
views
TypeScript - property does not exist on type (but it exists)?
I have a type of User that contains some user data. There's also a type of UsersResponse which basically is users: User[]. So far everything is easy. The problem is I have to pass UsersResponse as ...
0
votes
0
answers
223
views
Pre-commit MyPy hook: How to enforce explicit types for variables?
I'm using https://github.com/pre-commit/mirrors-mypy
I already checked https://mypy.readthedocs.io/en/latest/config_file.html for the available options but couldn't find something like ...
1
vote
1
answer
70
views
Add object to list in Umbraco controller
I am rather new in strongly typed languages and I working on a Umbraco controller that outputs some JSON with a list of dates.
"meetingTimes": [10:30, 11:30]
That works pretty well. Now I ...
0
votes
0
answers
52
views
Why can't variable names be deallocated for future use?
Why don't strongly typed programming languages, like the C family, for example, allow you to deallocate a variable name completely? That is, let's say you have a variable int x = 5. Why is there no ...
1
vote
0
answers
55
views
What is the benefit of Python being dynamically typed? [duplicate]
I've noticed that, with programming experience, I increasingly code in a way that mimics static typing*:
using Python's type hints along PEP 484, PEP 526, PEP 544, PEP 586, PEP 589, and PEP 591
...
0
votes
0
answers
40
views
How to only allow certain characters in python types? [duplicate]
How can I do something like this in python?
type something = "a" | "b"
const anotherthing: something = "c" // gives error
This typescript code does not allows to set the ...
1
vote
1
answer
78
views
Which to prefer? An enum class, or a nested unnamed enum type? [duplicate]
enum Color1 { red, blue, green }; // ok
// enum Color2 { red, blue, green }; // error, enum conflicts
struct Color3
{
enum { red, blue, green }; // ok, no conflicts
};
enum class Color4 { red, ...
1
vote
1
answer
2k
views
Typescript: get type of interface property based on interface key
I'm building a function that needs to update one field of an object at a time.
this object is defined through an interface T.
interface T {
a: TypeA;
b: TypeB;
c: TypeC;
}
This should be the ...
0
votes
1
answer
91
views
Strong typing dictionary contents
As part of a refactor of a large PowerShell program from PS2.0, functions and scripting quick practices to PS5.0, classes and programming best practices, I have been moving to strong typing everywhere ...
0
votes
0
answers
113
views
Strong typing a loop variable
I have been refactoring an app from functions and some bad scripting habits to classes and better habits, including strong typing of all my variables. And on that front I wonder what the "right&...
2
votes
2
answers
91
views
Any way to have strongly typed use of Navigator?
I have a MaterialButton with the following onPressed field:
onPressed: () async {
final int intResult = await showMyDialog(context, provider.myArg) ?? 0;
//...
}
Here is the showMyDialog ...
0
votes
2
answers
49
views
Typescript: Define array of <T> inside object
I'm coming from a JS background, and I'm learning TS.
I want to define an object with a single field, which is an array of strings, while taking advantage of strong typing.
let wrapper = {
things: ['...
-1
votes
2
answers
424
views
How to handle Typescript types in a model when there can be either of them?
Depending on the business logic by model will have the expanded entity or just the ids in the model.Example,
class Order{
id:string;
product:Product
}
class Product{
id:string;
name:string;
price:...
0
votes
0
answers
349
views
Type hint for Scikit-learn predictor
Is BaseEnsemble the correct superclass/interface for Scikit-learn predictors? I am new to Python, so maybe I am not thinking about the problem in the right way. If this were Java, this would be public ...
0
votes
0
answers
89
views
Store class with property and value into Config file
I have an api endpoint that returns these settings to a client HTML/JS that retrieve these data through either normal XML request or promise or async/await.
return new JsonResult(
new ...
0
votes
1
answer
71
views
How to implement domain strong typing using Java annotations
Problem
Working with anemic domain objects with multiple foreign keys stored as Long's. Trying to protect against transposing the values with some kind of strong typed domain types.
For example given ...
1
vote
2
answers
81
views
F#: How to represent a finite collection with strong typing?
I have a finite set of things all of the same type, and I wish to represent them in a strongly-typed way. I'd like to be able to manipulate the complete set and easily extract the elements. Here is ...
2
votes
1
answer
100
views
Implement a non-generic, static Factory method to create various Generic Classes from string input (without using "dynamic" type)
I have a set of classes which are defined and populated from parsed XML.
As part of this, I want to be able to dynamically instantiate collection classes for particular types, as specified by the XML (...
3
votes
1
answer
245
views
How to infer the right types of component's @Input
I'm looking for a way to infer the right types of a component's @Inputs.
So far, I'm only able to target the @Input properties using the following:
// Method in a service
setComponentInputs<T> (...
1
vote
2
answers
173
views
Add method specialization for all enumeration classes in C++
I have the following methods (broadly speaking) in my bitstream class:
class BitStream
{
void StoreBits( unsigned int data, unsigned int numBits );
public:
template<typename T>
...
1
vote
0
answers
25
views
Should I put the data types exposed via graphql in a package to avoid having to manually keep implementations in sync?
I'm building an application which consist of a handfull of containerized micro services. These services expose APIs (typically in graphql) so data can be transferred around to/from the correct ...
0
votes
1
answer
492
views
Extend the possible types of values of dictionary in TypeScript
I have a strongly-typed style object in my React Native project in TypeScript:
const styles = StyleSheet.create({
container:{
backgroundColor: 'red',
flex: 1
},
});
This works as normal ...
1
vote
1
answer
52
views
Contra/covariance and nested generics
I have a question on typing and contra/covairance.
Given the following classes
public class BoardItemsHolderRepository<THolder, TBoardItem> : DataRepository<IList<THolder>>
...
0
votes
1
answer
115
views
What is the process of runtime execution in java and how does JVM checks for array types at run time?
I was reading about generics and type safety and found that arrays cannot be generic in java. I also understood the covariant nature of arrays which directed me to the array store exception. I ...
1
vote
2
answers
153
views
Type-safe `a` tag helper
One of the most popular books on ASP.NET Core is Pro ASP.NET Core 3 by Adam Freeman.
In chapters 7-11, he builds an example application, SportsStore:
On the left, you'll notice the buttons for ...
2
votes
1
answer
372
views
Combining Typescript generics with any without losing type
This is related to a plugin I am building for the @nexus/schema library (type-safe GraphQL), but it is purely a Typescript typing issue.
I have a rules system where all my rules are derived form this ...