Use generics to type data property in GrayMatterFile interface #69
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I originally posted this in #67, but I realize that this is actually a bug when using Typescript.
I would like to type the
dataproperty inGrayMatterFileinterface. With a generic type, of curse. Could be done with this:Using default generics again, allow this to keep backward compatibility, if the
Tis declared after theI.This is because
objectdoes not allow accessing any custom properties, so effectively there are only a couple of properties that you can access when using this with Typescript. By using generics, developers are allowed to shape that property as they probably now the actual model ofdataproperty. By using defaults, if the developer does not provide a type, thendatawill beany, allowing to access any custom property.In
4.0.2we have used the type{ [key: string]: any; }because without validation that the parsed frontmatter matches the typeT, it is probably not a good idea to just cast the read-in type to T. It's just going to result in unexpected errors.You can add a validation + typecast logic on top of that type if you like.
At any rate, I'm going to close this as "won't fix". Cheers.