Questions tagged [typescript]

Programming language created by Microsoft. Open source, and compiles to JavaScript.

Filter by
Sorted by
Tagged with
0 votes
1 answer
74 views

How could I apply the strategy pattern to a react component?

For the following component, how would I extract the unit logic to allow for composition? In my actual app I am trying to reduce the amount of logic encoded in the component and I have decided that ...
Tyler Kasper's user avatar
0 votes
1 answer
118 views

How to Structure a Client Consuming an External API?

Context: I am building a site that is essentially just a UI for eBird's API. My first question is about naming. Wikipedia defines a client as "a piece of computer hardware or software that ...
Christopher Fimbel's user avatar
0 votes
2 answers
158 views

Local development for TypeScript library organized as mono repo with Lerna

I currently am developing a TypeScript shared library. The library needs to be imported in sections to minimize the imported bundle size, so I broke it up into packages with a monorepo with Lerna. ...
devleo's user avatar
  • 51
0 votes
2 answers
219 views

how to name functions that return booleans

I like the practice of naming boolean variables with a prefix like "is", "has", "should", or "can". But what about the functions that produce those results? ...
Ryan's user avatar
  • 137
-1 votes
1 answer
71 views

Optimal OOP security for PubSub - Should mutual consent be required?

I am implementing the Publish-Subscribe pattern. I have class Broker and interfaces IPublisher, ISubscriber. interface IPublisher { broker: Broker; publish(channel: Channel, data: object): ...
Michael Moreno's user avatar
1 vote
1 answer
71 views

Querying objects in a list by class type vs string identifier - Best practices

I have a class Character who has a list of Spell objects that he can cast. abstract class Spell { name: string manaCost: number abstract cast() { ... } } class InvisibilitySpell extends ...
Michael Moreno's user avatar
1 vote
0 answers
139 views

Should I extract my functions/types to new files in TypeScript?

Basically I have this class file which was getting quite large with functions and type definitions. Is it best practice to keep all the code that works on the same aspect in the same file for "...
Eliaz's user avatar
  • 21
-1 votes
1 answer
228 views

Right using monorepo for ts-node scripts

I have a repository with various tools/scripts written with ts-node and used in many projects. Initially, it contained several automation/analytics scripts, one package.json file at the root, and one ...
Dem0n13's user avatar
  • 99
0 votes
1 answer
84 views

How to use Typescript to tag certain fields as Personally Identifiable Information, so they can be obfuscated in logging, etc

Using Typescript, if we imagine an entity like this: interface Order { id: string; address: string; } Now I would like to design a system where we can process entities feeling confident that the ...
Jon Lauridsen's user avatar
0 votes
1 answer
97 views

design pattern advice on method of "injecting" html in React

Say I have a ReactNode post: const Post = (props:{content:string}) => { return( <div> {textProcessor(props.content)} </div> ) } the role of textProcessor is to 1. to ...
Leftover Salad's user avatar
1 vote
3 answers
344 views

Best way to prevent people going overboard with Typescript?

I like typescript, and I want to add it to some projects in the company. Thing is, it is rather powerful, probably too powerful for it's own good at times. From experience, it doesn't take long until ...
user avatar
1 vote
0 answers
28 views

Peer dependencies vs. (re-)exporting dependencies in abstract hierarchy (framework)

Say I have a module storage-abstract, which contains abstract logic (framework) for storing key/values. I also have some (arbitrarily many) implementations of this abstraction, each in a separate ...
Remirror's user avatar
  • 129
1 vote
1 answer
107 views

What to use in B's configuration: A or A's configuration?

When I have a class A in a module amod as follows // module amod type AConfig = { x: number; // ... }; class A { constructor(public config: AConfig) {} // ... } export {A, type AConfig} ...
Remirror's user avatar
  • 129
0 votes
0 answers
56 views

How to efficiently retry/reprocess errors on a multi-actions service?

I have a service responsible for processing orders from an e-commerce, within this service, there are several actions that need to be performed, some in order and some not. Example: Notify the user ...
Thiago Dias's user avatar
0 votes
1 answer
162 views

Is it premature optimisation to test a condition to skip a small loop?

I have an array of entities which have an id property: entities: {id: string|null}[] = []; All items in the array are guaranteed to have id !== null and the array will not exceed 100 items. Now I may ...
lampshade's user avatar
  • 103
0 votes
0 answers
54 views

Design a push events service at code level

In a large monorepo containing multiple micro-services, I need to build a service to push various events to several of them. The event interface differs from service to service(one will have filed_5 ...
pAndrei's user avatar
  • 101
2 votes
3 answers
246 views

Static types and dependencies in trunk-based development

There're certain things in code that can't be conditional. Two very common examples: static types dependencies. You can't naturally enable/disable a feature that implies a new dependency or a new ...
Ivan Kleshnin's user avatar
1 vote
0 answers
45 views

Separating Typeorm Entities from Domain Entities [duplicate]

Is it a bad approach to add domain logic inside typeorm entities if following the domain model pattern to organize business logic. import { Entity, PrimaryGeneratedColumn, Column } from "typeorm&...
Maak's user avatar
  • 67
1 vote
6 answers
1k views

TDD: testing an abstract class. Test all concrete classes or create mock sub-class to test the behavior?

Well, I'm developing a personal project using TDD and Clean Architecture, and I chose to build an abstract class to gather all use-cases behavior in one single class. My question is: What you think ...
Gabriel Antonio's user avatar
0 votes
1 answer
333 views

What should I write before the tests in Clean Architecture with TDD?

Well, we all know the main concept of TDD: write a failing test, then implement the testing component to make your test pass. I've been working on a personal project in Clean Architecture following ...
Gabriel Antonio's user avatar
-3 votes
1 answer
783 views

Is it a bad practice to pass instance as function parameter? [closed]

So I am learning how to use the factory function in javascript/typescript. I created a to-do list project where I had to pass the instance as a parameter to the function. I wanted to ask is it a bad ...
Mubashir Waheed's user avatar
0 votes
0 answers
106 views

Identify Domain Entities for a search tool interface - Clean Architecture

I am pretty new to Clean Architecture / Onion Achitecture, I actually not sure if the 2 terms are exchangeable. So it pretty clear to me, considering is a few days I am reading and study about it. It ...
Carmine Tambascia's user avatar
0 votes
3 answers
242 views

Decoupling command and receiver in command pattern

I'm writing a simulation of a car that can receive commands and act on them and I'm trying to implement it using the command pattern. class Car { move() { console.log('move'); } } interface ...
Epsilon's user avatar
13 votes
6 answers
5k views

Is it OK to have type aliases for primitive types in TypeScript?

In a project that my team is working on, we have type aliases for primitive types. The reason, I got from my team, for this is to have consistency across the codebase. For example: type FruitName = ...
vighnesh153's user avatar
0 votes
1 answer
179 views

Changing the generic type when chaining operations

I have a class where I want to chain certain operations. The class look something like this class MyClass<T> { create<X>(fn: (_: T) => X): MyClass<X> { ... ...
pratZ's user avatar
  • 119
-1 votes
2 answers
183 views

What to do when forced to initialize functions in createContext

What do I do in this situation? Do I just initialize dummy functions in place of signup, login, and logout? It seems so unnecessary since they are defined in AuthProvider. type AuthContext = { ...
Sebastian Nielsen's user avatar
0 votes
1 answer
2k views

Where should interfaces/model definitions live? -- Clean architecture

I'm working on a Node project (in typescript) that features a variation of clean architecture. I have a pretty clear understanding of the different layers of an application and how they depend on each ...
GHOST-34's user avatar
  • 111
0 votes
1 answer
509 views

Removing successful cases from SQS batch. Is it wrong?

I have been working on a lambda that is triggered by a SQS with a batch (let's say of 5) and has automatic retry enabled. The most common problem with this kind of setup that I have seen is that, if ...
Javier Bullrich's user avatar
0 votes
1 answer
69 views

Extending TypeScript while being futureproof [closed]

C#10 Has extended the property pattern ability, I think it began since #7 EG: { Car: { Interior: pattern } } Meaning you can do some nice pattern matching even in if statements if (car_var is ...
Pogrindis's user avatar
  • 109
2 votes
1 answer
963 views

Is usage of "global" types in typescript considered bad practice?

I'm fairly new to strongly typed languages, coming from the "jungle" world of JS and PHP. In my current Typescript project(React-Typescript), i'm struggling with a very fundamental question: ...
i.brod's user avatar
  • 125
2 votes
6 answers
598 views

Liskov Substitution Principle Inconsistency? How to figure out?

Liskov Substitution Principle states: "if S is a subtype of T, then objects of type T may be replaced with objects of type S without altering any of the desirable properties of the program (...
Rodrigo Branas's user avatar
1 vote
5 answers
1k views

Clean architecture, Login UseCase with OAuth breaking Single responsability principle

I'm building a login system using OAuth to integrate with various social networks. The flow is very simple, first, the user is redirected to their social network to grant permission to read the ...
Thiago Dias's user avatar
1 vote
2 answers
234 views

Is it a good/bad practice to create overlapping types?

Here's a simple example: an API returns a list of possible options with the following structure: { "familystatus": [ { "Id": 1, "Name": "single&...
noamyg's user avatar
  • 121
4 votes
3 answers
499 views

Steps to achieve the best code quality?

Currently I'm on a project where my objective is to obtain the best code quality. We have unit, integration & e2e testing. The e2e tests are written by the Business team in cucumber and these ...
Imaky's user avatar
  • 148
6 votes
6 answers
455 views

Should a Car object be responsible for checking if it is able to run on certain Road objects?

I have 2 kinds of objects in my system, Car and Road. A Car can be of various types and have properties like tire, tire size, engine, etc. A Road can be of multiple types and have properties like ...
Thiago's user avatar
  • 169
0 votes
1 answer
60 views

Pros and Cons of placing a method as static in the class or as private function outside of class when it doesn't utilize this?

I use TypeScript. When I write the below code ESlint throws this error: https://eslint.org/docs/rules/class-methods-use-this export class { constructor() { const result = doCalculations() ......
demiculus's user avatar
  • 109
0 votes
1 answer
93 views

How are discriminated unions (e.g. in TS) used in messaging schemes?

In the documentation about discriminated unions in typescript, here it says: Discriminated unions are useful for more than just talking about circles and squares. They’re good for representing any ...
Bahaa's user avatar
  • 127
2 votes
1 answer
318 views

When should a variable be declared / Value assigned?

So we have quiet a big TypeScript codebase at work, where each method resides in its own file. Now in a lot of those methods the variables are often declared all together at the top/beginnng of the ...
Getter Jetter's user avatar
0 votes
2 answers
106 views

Language-independent explanation of "registration" by the IoC container

I've been reading quite a lot on the concept of Inversion of Control (IoC) and Dependency Injection (DI), and I've seen that DI often relies on an IoC container, which among other roles has to ...
Etfrerrr's user avatar
5 votes
3 answers
495 views

Qualms over the 'flexibility' advantage of using dependency injection

I'm currently studying dependency injection and I'm having some issues with the so called 'flexibility' advantage of using dependency injection. The flexibility advantage is mentioned in post ...
Etfrerrr's user avatar
0 votes
1 answer
225 views

Is a "wrong" to return anything else besides `this` in a constructor?

Is a "wrong" to return anything else besides this in a constructor? Take this TypeScript Queue sample: class Queue { private buffer: string; constructor(buffer: string = "&...
Daniel Santos's user avatar
2 votes
2 answers
951 views

Domain driven design model version

sorry for the long question, but I love DDD but there is a problem I can't seem to resolve. We have a problem when adding business rules to the aggregate root. Our architecture is mostly like this ...
bachibusuc's user avatar
2 votes
0 answers
1k views

Setting width/height of unknown images for lazy loading

I have a site where users upload images & can all be viewed on a single page by other users. I want to lazy load images as the user scrolls instead of loading all initially. This is easy enough, ...
Avery Ferrante's user avatar
-1 votes
1 answer
123 views

Angular: send form or object?

My question is about clean code or best practice. I have a big formGroup and in my opinion its more difficult to map the form to an object than to send the form to the backend. best regards
yoyo's user avatar
  • 13
3 votes
0 answers
330 views

How do I architect reordering in database?

I'm building a todo list app and using react-beautiful-dnd on the frontend to allow users to reorder tasks. I'm trying to work out how to persist changes to the database when a user reorders items. ...
mrseanbaines's user avatar
2 votes
2 answers
346 views

Should base class include unused properties of sub class?

I was looking at the angular documentation and noticed this code: export class QuestionBase<T> { value: T; key: string; label: string; required: boolean; order: number; controlType: ...
p32094's user avatar
  • 177
0 votes
1 answer
128 views

Use nullable or undefineable as object type?

This come with a debate with my colleague that I'm using nullable object type. type Value = Node | null const [v0, setV0] = React.useState<Value>(null) const [v1, setV1] = React.useState&...
Mengo's user avatar
  • 579
0 votes
1 answer
109 views

Preferred architecture for tree stuctured data (Angular/Typescript)

After reading some of "Clean Architecture" I got the idea that perhaps I should reconsider the way I have architected a chart of accounts/accounts object in an accounting program. I have ...
GGizmos's user avatar
  • 103
28 votes
13 answers
9k views

Ways to explain code when told it doesn't make sense

As a programmer I have found my code frequently elicits the reaction "I don't understand". Whenever I get this response I try my best to explain my code patiently, and not make anyone feel ...
yeerk's user avatar
  • 500
4 votes
3 answers
2k views

Testing unexpected inputs for unit tests and loops?

I'm working on an enterprise that has some Angular/Typescript projects and to avoid repeating code (basically copying and pasting) between them, we decided to go for Monorepo and start write an util ...
dev_054's user avatar
  • 151