TypeScript: Make stricter Required

When you have a type with optional props, then to make those props required you can use Required like so:

type TTypeWithOptional = { 
  a?: string;
  b: string | undefined;
  c?: string | null 
};

type TRequired = Required<TTypeWithOptional>;
/*
// result
type TRequired = {
    a: string;
    b: string | undefined;
    c: string | null;
}
*/Code language: TypeScript (typescript)
Continue reading
Posted in TypeScript | Tagged , , | Leave a comment

BrowserPriorityTimers npm module

I’ve prepared npm package with the BrowserPriorityTimers class. It gives you ability to use timers that will not be throttled by the browser. This maybe useful at times when you want to keep your timers execute without interruption even when the tab is inactive. You can install it with the npm i browser-priority-timers command.

Continue reading
Posted in npm modules, TypeScript | Tagged , , , | Comments Off on BrowserPriorityTimers npm module

TypeScript: Add typing to the lodash entries helper

This is a followup post to the other one where we’ve added types to the lodash keys helper. This time we will add types to the entries helper, which is an alias to the toPairs helper, where we can also benefit from the proper typing of the returned list of key, value pairs. Let’s start with the same setup as in the other post:

Continue reading
Posted in TypeScript | Tagged | Comments Off on TypeScript: Add typing to the lodash entries helper

TypeScript: Add typing to the lodash keys helper

With lodash (Version 4.17.21 at time of writing) and @types/lodash (Version 4.14.191 at time of writing) using the keys helper will lose the type information of the given object.

Continue reading
Posted in TypeScript | Tagged | 1 Comment

TypeScript: custom type guards

Type guards are tools for type narrowing in TypeScript. You may have used some of those features e.g. typeof, instanceof, in etc. already but I want to describe writing guards for types created by you.

Continue reading
Posted in TypeScript | Tagged , | Comments Off on TypeScript: custom type guards