You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Right now there is no way to set the type of file.data except with as.
In general, as in TypeScript conveys incomplete typings for external libraries or insufficient time to fix an issue with types.
You want to reduce as in a code base as much as possible in order for it to be a red flag when it comes up.
However, with I/O we tap outside of types and telling our code which shape a resulting object will have is necessary.
Other libraries achieve this by simply exposing a generic type param on classes or methods like this:
interfaceMyFrontMatter{title: stringdate: string}const{ data }=matter<string,{},MyFrontMatter>(fileContents);const{ title, date }=data;// statically typed, TS knows that title is a string, not 'any'
The advantage is that this conveys intent, that the typings are as clean as they can be.
It would be great if you could add this as a third param to the types!
declarefunctionmatter<Iextendsmatter.Input,Oextendsmatter.GrayMatterOption<I,O>,MatterShapeextendsRecord<string,unknown>// not sure about this>(input: I|{content: I},options?: O): matter.GrayMatterFile<I,MatterShape>// ...interfaceGrayMatterFile<IextendsInput,MatterShapeextendsRecord<string,unknown>>{data: MatterShapecontent: stringexcerpt?: stringorig: Buffer|Ilanguage: stringmatter: stringstringify(lang: string): string}```
The text was updated successfully, but these errors were encountered:
As I've encountered this limitation multiple times, I created a small wrapper for gray-matter using zod to actually validate front matter and get proper type inference, e.g.:
Right now there is no way to set the type of
file.data
except withas
.In general,
as
in TypeScript conveys incomplete typings for external libraries or insufficient time to fix an issue with types.You want to reduce
as
in a code base as much as possible in order for it to be a red flag when it comes up.However, with I/O we tap outside of types and telling our code which shape a resulting object will have is necessary.
Other libraries achieve this by simply exposing a generic type param on classes or methods like this:
The advantage is that this conveys intent, that the typings are as clean as they can be.
It would be great if you could add this as a third param to the types!
The text was updated successfully, but these errors were encountered: