For application code, is it advisable to put Redux store types in state-slice.d.ts
files?
I inherited a codebase where all the core types have been placed in .d.ts
definition files, but this is contrary to how I’ve come to understand the role of definition files.
My understanding is that definition files are for when you’re writing a library (which is a convenient place for authors to centralize an interface to define our library's API that we're exposing to consumers), but is ill-suited for application code since data can change rapidly.
That said, I’ve not been able to come up with a good argument against .d.ts
files per se - from a functional standpoint, I don’t see the difference of referencing a type
declare namespace Domain {
interface State {
// ...
}
}
from a definition file, or explicitly importing a file from a file that has
export interface ReduxState {}
To add bit more context, since this is a bit of a peculiar question - I’m currently working on a new project that needs to share these core types from the existing codebase I inherited, and I’m trying to move these types into a separate package.
Unfortunately I’ve been running into issues with importing and exporting these types - it seems that I have to (note the export
s)
/* package A */
export declare namespace StateA { }
/* package B */
import { StateA } from 'packageA/StateA'
const stateA: StateA = {};
instead of TypeScript automatically picking it up.
/* package B (originally) */
// stateA.d.ts
declare namespace StateA {}
// test.ts
const stateA: StateA = {};
I’ve tried changing the typeRoots
field in the tsconfig.json
file to see if I can avoid having to insert import
statements everywhere, to no avail.
I was thinking that this might a good opportunity to convert all type definition files to ordinary type files, but I just can’t come up with a good reason apart from "I don’t think we should be using type definition files" and "I can’t get the types in the definition types to work like they do now".
So my questions are
- What are the benefits of using type definition files over defining types in an ordinary
.ts
file? - Should we be writing our own definition files when working with consumer facing application code?
- Am I thinking too much and it really doesn't matter?
I do apologize in advance if this question is a bit open-ended, I hope this isn't ill-suited for the StackOverflow community (or if it is, what would be a good forum to ask?). Thanks!