Message from JavaScript discussions

November 2020

— Like, the original function requires the entire fs when the underlying module only needs.. say fs.writeFile and fs.readFile

Message permanent page


Basically, when you pass an argument to a type in TypeScript, TypeScript can compare their shapes rather than their identity

— Do I always have to Pick<> then?

— So in this case, you can do something like:

{ readFile: (path: string, enc: string) => string & (path: string) => Buffer }

(which granted is quite a big signature, BUT it grants you the ability to provide only the API your function needs, and not the entire fs library)

Message permanent page

— Another cool benefit is that if node stdlib has breaking changes, you can detect if your functions are affected by it, since TS will actually compare the structures

Message permanent page

— 🤔 okay

— Nice

— Pick is also fine, as long as you can construct a manageable substructure like the above

— But then you're depending directly on another type essentially

— So if you have a function that takes this type, passing fs to it is completely fine

— Aka: fs is "assignable" to that type

— Which doesn't mean that that type is "assignable" to the fs type