-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: improve metadata value types #194
Conversation
Current dependencies on/for this PR:
This stack of pull requests is managed by Graphite. |
80fd0da
to
5f35752
Compare
📦 Bundle size (Angular v15)Git ref: 8508c47
|
📦 Bundle size (Angular v16)Git ref: 8508c47
Base size data is not available yet. Try again when the CI/CD has finished running on main branch |
📦 Bundle size (Angular v17)Git ref: 8508c47
|
5f35752
to
8508c47
Compare
export interface MetadataValues { | ||
[key: string]: unknown | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was giving issues, as it's a specific type of object
. An object that implements an interface without index signature wouldn't comply with this. Despite yet being valid metadata values. So going for a broader definition
|
||
export type MetadataRouteStrategy = <T>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't make sense to specify type here, given the strategy should load some object from route, without caring what the object contains. It's not its responsibility
🎉 This PR is included in version 1.0.0-alpha.31 🎉 The release is available on: Your semantic-release bot 📦🚀 |
In the metadata set by route + service E2E test, the code to use the
MetadataService
was a bit awkward in the types sense. We had to cast tounknown
and then toMetadataValues
. Not a great DXRethinking the types referring to metadata values and their usages to provide as good DX as we can
Changes:
MetadataValues
be justobject
. Which is anything other than a primitive:This allows metadata values to be whatever JSON object unless you specifically opt-in for some types. There's no specific JSON type in Typescript So that's the best option AFAIK. Not using
unknown
as that includes primitives and we're sure we want an object. Indeedunknown
allowsundefined
too. Not usingany
as it's a bad practice + same issues asunknown
In. Consumers can declare data to put as argument in a separate variable or useMetadataService
, make theset
method generic to allow to opt-in to type-safe values argumentsatisfies
MetadataRouteStrategy
type returnMetadataValues
tooMetadataRouteData
generic to allow specifying type of route data. Defaulting to broadMetadataValues
akaobject
in case you don't want to opt-in for safe types.This allows to:
XMetadata
&X
types to declare type safe metadata values. LikeOpenGraphMetadata
&OpenGraph
.MetadataRouteData<OpenGraphMetadata & TwitterCardMetadata>
to ensure route data is properly typed. Either when declaring a var type or usingsatisfies
.MetadataValues
/object
, so it's open for extension but closed for modification. Consumers can specify a stricter type if they want to ensure types are ok or the IDE to help them write them.This doesn't allow to:
OpenGraph.type
orTwitterCard.card
) don't match. Because the JSON types those fields asstring
. You can't useas const
in there. Yet. import ConstJson from './config.json' as const; microsoft/TypeScript#32063