Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ const p: WithContext<Person> = {
};
```

### Reusing a property's type

Every Schema.org type is also exported as a concrete "leaf" interface (e.g.
`OrganizationLeaf`) alongside its union alias (`Organization`). Indexing the
union alias to grab a single property's type fails, because the alias also
includes an id-reference `string` that has no properties. Index the leaf
interface instead:

```ts
import type {OrganizationLeaf} from 'schema-dts';

// Previously required Exclude<Organization, string>['image'].
type OrgImage = OrganizationLeaf['image'];
type OrgLogo = OrganizationLeaf['logo'];
```

### Merging multiple concrete types

Some Schema.org objects can legitimately carry multiple concrete `@type` values.
Expand Down
15 changes: 15 additions & 0 deletions packages/schema-dts/test/leaf_property.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type {OrganizationLeaf} from '../dist/schema';

// A property's type can be extracted from the concrete leaf interface. Indexing
// the union alias `Organization` instead would fail, because that alias also
// includes an id-reference `string` with no properties.
type OrgImage = OrganizationLeaf['image'];
type OrgName = OrganizationLeaf['name'];

const _image: OrgImage = 'https://acme.com/logo.png';

const _name: OrgName = 'Acme Corp';

// The extracted type still rejects invalid values.
// @ts-expect-error image is not a number.
const _bad: OrgImage = 42;