TypeScript: deep partial?

You can simply create a new type, say, DeepPartial, which basically references itself (updated Jan 2022 to handle possible non-objects): type DeepPartial<T> = T extends object ? { [P in keyof T]?: DeepPartial<T[P]>; } : T; Then, you can use it as such: const foobar: DeepPartial<Foobar> = { foo: 1, bar: { baz: true } … Read more

What are the differences between the private keyword and private fields in TypeScript?

Private keyword The private keyword in TypeScript is a compile time annotation. It tells the compiler that a property should only be accessible inside that class: class PrivateKeywordClass { private value = 1; } const obj = new PrivateKeywordClass(); obj.value // compiler error: Property ‘value’ is private and only accessible within class ‘PrivateKeywordClass’. However compile … Read more

How do I decide whether @types/* goes into `dependencies` or `devDependencies`?

Let’s say you’re developing a package “A” that have @types/some-module package in devDependencies. For some reason you’re exporting the type from @types/some-module: import { SomeType } from ‘some-module’; export default class APackageClass { constructor(private config: SomeType) { // … } } Right now TypeScript consumers of package “A” are unable to guess what SomeType is, … Read more

Typescript never type condition

I believe this is a consequence of the distributive property of conditional types in Typescript. Essentially, (S | T) extends A ? B : C is equivalent to (S extends A ? B : C) | (T extends A ? B : C), i.e. the conditional type distributes over the union in the extends clause. … Read more