Skip to content

Author: admin@master

Casting a number to a string in TypeScript

Which is the the best way (if there is one) to cast from number to string in Typescript? In this case the compiler throws the error: Type ‘number’ is not assignable to type ‘string’ Because location.hash is a string. So which method is better? Answer “Casting” is different …

Detect click outside React component

I’m looking for a way to detect if a click event happened outside of a component, as described in this article. jQuery closest() is used to see if the target from a click event has the dom element as one of its parents. If there is a match the click event belongs to one of the children and is thus

How can I download a file using window.fetch?

If I want to download a file, what should I do in the then block below? Note: The code is on the client-side. Answer I temporarily solve this problem by using download.js and blob. It’s working for small files, but maybe not working for large files. I think I should dig Stream more.

Square Brackets Javascript Object Key

Can anyone explain how the why/how the below method of assigning keys in JavaScript works? return: Answer It’s the new ES2015 (the EcmaScript spec formally known as ES6) computed property name syntax. It’s a shorthand for the someObject[someKey] assignment that you know from ES3/5: is syntactic su…

Mapping object to key=value string in one line

Is there a way to convert this object: To a string with the following format? Much like mapping an object to a query string, where each property is a query parameter. I can do it like this: But I was looking for a one-line solution. Is this possible? PS: I cannot use jQuery and any of it utility functions. Th…