Comments (3)
I wrote up an explanation here about the structure of the type system, but basically, we have something like:
declare type ThingBase = {
"additionalType"?: URL | URL[];
"alternateName"?: Text | Text[];
// ...
};
// This is conceptually true but in practice, it is inlined:
export declare type ThingLeaf = {
"@type": "Thing";
} & ThingBase;
export declare type Thing = ThingLeaf | Action | CreativeWork | Event | Intangible | MedicalEntity | Organization | Person | Place | Product /* | ... */;
So Thing
itself is not just ThingLeaf
, but any subsequent child type. The discriminated unions progress recursively to cover all Schema.org types. e.g.
export declare type Organization = OrganizationLeaf | Airline | Consortium | Corporation | EducationalOrganization | FundingScheme | GovernmentOrganization | LibrarySystem | LocalBusiness | MedicalOrganization | NewsMediaOrganization | NGO | PerformingGroup | Project | SportsOrganization | WorkersUnion;
... and so on.
Under the current type system, Person
is part of Thing
's union, and thus it extends it.
You can try it out with WithContext
, which is supplied by schema-dts and you can import it. It's written as such:
export declare type WithContext<T extends Thing> = T & {
"@context": "https://schema.org";
};
With that, you can write things like:
import { Person, WithContext } from "schema-dts";
const t: WithContext<Person> = {
"@context": "https://schema.org",
"@type": "Person", // "Patient" also works here
"name": "happy"
};
from schema-dts.
#28 suggested another use-case where something that looks like this.
My general concern with exporting intermediate types is just that it ties up future development. Right now the API boundary is just the final types (Thing
, Person
, etc.) and everything in between can change in a breaking way.
As schema-dts matures, I'll be more and more comfortable doing this. Worth looking at this now.
I'm curious, though, why not just use Thing
here? It seems like it should just work if WithId
was defined as:
export type WithId<T extends Thing> = T & Identified;
That's what we do when we define WithContext
right now.
from schema-dts.
But for example Person
and Organization
do not extend Thing
...so how could that be correct?
from schema-dts.
Related Issues (20)
- Bundles to Webpack when not needed HOT 2
- Allow Role to be used for use cases like defining dates HOT 10
- Feature Request: schema-dts-gen --file=my-custom-schema-file.nt HOT 2
- BUG: schema-dts-gen and custom context - prefixes other than "schema" are ignored during generation of code for types HOT 4
- Feature Request: Add safeJsonLdReplacer() from react-schemaorg to schema-dts-gen generated code
- WithContext<T> should be assignable to T HOT 5
- hiringOrganization HOT 3
- Error TS4023 when importing types HOT 5
- Generate types for external schema HOT 10
- Person does not inherit correctly from interfaces PersonLeaf -> PersonBase HOT 10
- Increasing HTML size HOT 1
- Support for Multi-Typed Entities HOT 10
- Installing as dependency (instead of devDependency) makes final bundle big HOT 2
- Add an install option for pending HOT 3
- WithContext: Type '"https://schema.org"' is not assignable to type '"https://schema.org"' HOT 2
- sameAs as an array HOT 2
- Wrong type picked on using WithContext HOT 1
- '@type' as array HOT 3
- Rich Result Text shows "offer" error when using Motorcycle type
- TS4058: Return type of exported function has or is using name WebSiteLeaf from external module ... but cannot be named HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from schema-dts.