-
Notifications
You must be signed in to change notification settings - Fork 34
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
The WithContext type is invalid #98
Comments
Hm, I'm not sure this makes the type invalid per se, since intersections distribute over unions,
We can make this more explicit by saying: export declare type WithContext<T extends Thing> = Exclude<T, string> & {
"@context": "https://schema.org";
}; Can I ask if this is triggering a bug or bad behavior here? It is intended behavior for |
Just having some trouble with the following utility function: import { Thing, WithContext } from 'schema-dts';
export const addContextToSchema = <T extends Thing>(thing: T): WithContext<T> => {
return {
'@context': 'https://schema.org',
...thing // error: cannot be destructured as Thing may not be an object.
};
}; This is my temporary workaround: import { Thing, WithContext } from 'schema-dts';
// TODO: Remove this type when this issue has been resolved
// https://github.com/google/schema-dts/issues/98
export type Schema = Exclude<Thing, string>;
export const addContextToSchema = <T extends Schema>(schema: T): WithContext<T> => {
return {
'@context': 'https://schema.org',
...schema
};
}; My hope is that you can export a variant of Looking at schema.org, I think these top-level 'things' are called 'types':
So perhaps: export declare type ThingType = Exclude<Thing, string>;
export declare type WithContext<T extends ThingType> = T & {
"@context": "https://schema.org";
}; Or maybe:
|
Does this happen with just "WithSchema" or perhaps also with other things? I thought I remember something about this being the case, but regardless, it would probably be nice to limit the declarations that could get corrupted by the string inclusion. |
The
WithContext
type accepts allThing
types:However,
Thing
allowsstring
values. For example:It's not possible to intersect
string
andobject
, which makes theWithContext
type invalid.The text was updated successfully, but these errors were encountered: