Skip to content
0:00 / 0:00

#typescript#declarationfiles#d.ts

Don't put your types in .d.ts files

Matt Pocock

429K subscribers • 9 videos on Hallway

YouTube
  • Recommended by 2 people: 2 in engineering communities.
  • Presented by Matt Pocock
  • Linked in the descriptionmicrosoft/TypeScript
  • An opinion. It argues one side of a question engineers disagree on.

Intermediate. Assumes working knowledge.

165K views3 years ago2 recommendations

Programming languagesTools and practiceTypeScript

Matt Pocock explains why putting runtime code or general types in declaration files can cause issues and how to structure them instead.

TypeScript doesn't support this pattern: github.com/microsoft/TypeScript/issues/52593#issuecomment-14… Become a TypeScript Wizard with my free beginners TypeScript Course: totaltypescript.com/tutorials/beginners-typescript Follow Matt on Twitter twitter.com/mattpocockuk Join the Discord: mattpocock.com/discord

Why it's here

2 people recommend this

What people say. Recommended as a clear explanation of why runtime code should not go into declaration files and how to handle global types.

u/ElPirer97in r/typescript • Recommends

On “Using "new" keyword from .d.ts file causes "Error: Can't walk dependency graph: Cannot find module"”

You should not put runtime code in .d.ts files, they should only define types/interfaces and be paired with a corresponding .js files. If you need to import types from a file into another just use a regular .ts file, if it only contains types/interfaces it will get completely erased when transpiling. This video by Matt Pocock explains it beautifully.

u/Matt23488in r/typescript • Recommends

On “Need triple slash reference for global variables ??”

link to this video I think if you still want the type globally accessible, you should be able to throw it in the global scope like this: ``` declare global { type TodoAny = any; } ``` You should be able to put that in any `.ts` file and it should make the type available globally. Just be careful if you do this with an interface, because of declaration merging.