Skip to content
Advertisement

Tag: typescript

Using union type as function parameter

I have a type defined like so: where FieldValue is a union of types: I have a variable object where I then declare the functions set by the above types: This produces an error where the values in the object are not of type FieldToAction, I kind of understand why since I am now constraining the parameter to be a

Best practices for declaring or assigning types

I’m a beginner with TypeScript (Day 1), but have worked on above average JavaScript projects. I’m looking for advice about some “best practices” and standards when it comes to declaring or assigning types in code. At the moment, I find myself doing it for literally everything which is essentially making my code unreadable. For example, consider this: This is a

Typescript returning array of objects with unknown keys

I’m new to TypeScript, and trying to slowly migrate an existing JS (react) project to TypeScript. I have a function executing a query: And I’m calling this in numerous locations: The last line gives an error, because the field ‘cnt’ is unknown. How to best solve this? The return type is always Object[], but the content ob the object depends

Pushing array into an array of array in TS

I am coming from Python, and am not entirely familiar with the ‘proper’ JS/TS way of doing things. I am looping through the elements of a set, and I am pushing lists of some of the elements onto a 2D array. I am getting the following error TypeError: Cannot read properties of undefined (reading ‘push’). What am I doing wrong,

Advertisement