I currently have a string and set of substrings/search strings which I want to effectively search in the given string. This is what I have currently: This approach works, however I am looking for a more compact and efficent way of searching for substrings in a given string. Edit: I am currently using the following way: Let me know if
Tag: typescript
Map a union to a string literal union of the type names
I have a type like this (in this case, the user would be a custom interface): now, I would like to have a type of I’ve tried to search around myself for a while, but I have not been able to find a way to get the name. I tried to do ${ArgumentTypes} but that didn’t work. I’m pretty new
Convert json from file to map not working
i have this code which gives the error “Error: Uncaught (in promise): TypeError: this.weekMap.get is not a function”. Why weekMap is not recognized as a Map object? The Json file contains: } Answer You are not converting the variable weekMap to a Map object correctly. Try the Object.entries function:
Variable ‘value’ is used before being assigned
My code: And got this: I found this very odd, as I had seen other similar problems that either used a callback or a Promise or some other asynchronous method, while I used just a synchronous for loop. ———————————- Some update ———————— Answer Use the non-null assertion operator to ensure that “its operand is non-null and non-undefined in contexts where
React.js builds with Vite does not include service-worker.ts
I am using Vite to build an SPA with React (typescript), and I am trying to register a service-worker. I am registering the script as type module, and service-worker.ts sits at src/web-worker/service-worker.ts. There is also a tsconfig.json at src/web-worker Everything works in Dev, but when it’s built, src/web-worker/service-worker.ts is not replaced with anything equivalent. Any suggestions? index.html src/web-worker/service-worker.ts src/web-worker/tsconfig.json Answer
How to watch public directory in Vite project for hot-reload?
I have a react project configured with Vite. Hot reload works great, but I use react-i18next for multiple language support and this is my structure: When I change the translation.json files, Vite doesn’t watch it, and I have to refresh the page to see the changes. Is there a way to tell Vite to watch all the files in the
Expand a variable in a MongoDB aggregation pipeline
In a Typesctipt code, I would like to use a varible value in an aggregation pipeline in MongoDB; the problem is that the “keyToCheck” field is a variable that is set by the Typescript code and, therefore, can change based by many conditions. Is there a way to expand the variable “keyToCheck”? I have tried $$keyToCheck, $keyToCheck with no result
typescript template literal in interface key error
Typescript v4.4.3 Reproducible Playground Example — What i’ve tried: I’ve confirmed that id in my code is a string. This happens when running tsc not just in VSCode warnings Is there some restrictions of using template literals or anything else I can investigate to diagnose this? Answer The issue is that computed keys of types that are not single literal
How to access object property with dynamic key in Typescript?
In my component Product.tsx I receive an object via API call that could look like this: I then render each variant as a row in a table where each column displays the price, size etc. as editable input fields. onChange of each input triggers updateVariant(variant.id, key, e); where key is one of the keys of a variant and e is
How to fix React.useEffect and useCallback circular loop when updating a state?
I’m fetching some data from an API, but since I am using this data to update a state within a useEffect, this state becomes a required dependency, which causes a circular loop. state is updated -> useEffect is called -> state is updated … I read a lot of answers and articles about that, but I couldn’t find an answer