Post

TypeScript type of the day ``` export type LazyType<T> = T & { [K in "" as never]: LazyType<never> }; ```

0 reposts9 likes

Hi, I think there's a lot wrong with me, but in this case I'm just caring for users with potentially weird edge cases too much 😅

It prevents a type from being evaluated when it is emitted into .d.ts files by the compiler. We have a few interfaces that can be augmented in userland, and if the library build process - or the build process of a userland monorepo utility library - compiles this type (that depends on augmented

interfaces) too soon, it would be inlined based on the augmentation in place at compile time of the library, instead of at the compile time of the "final consuming app". So adding this self-referencing loop here prevents that inlining until the last moment. It makes the type lazy.