Skip to content

Commit

Permalink
Merge pull request #130 from saiichihashimoto/version-3
Browse files Browse the repository at this point in the history
V3
  • Loading branch information
kodiakhq[bot] authored Jul 18, 2023
2 parents 6f2953b + fabd2af commit f0fda21
Show file tree
Hide file tree
Showing 3 changed files with 351 additions and 182 deletions.
72 changes: 69 additions & 3 deletions packages/types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default config;
type Values = InferSchemaValues<typeof config>;

// Import Product type into your application!
export type Product = Extract<Values, { _type: "product" }>;
export type Product = Values["product"];
/**
* Product === {
* _createdAt: string;
Expand Down Expand Up @@ -135,7 +135,7 @@ export default config;

type Values = InferSchemaValues<typeof config>;

export type Foo = Extract<Values, { _type: "foo" }>;
export type Foo = Values["foo"];
/**
* Foo === {
* _createdAt: string;
Expand Down Expand Up @@ -203,7 +203,7 @@ export default config;

type Values = InferSchemaValues<typeof config>;

export type Foo = Extract<Values, { _type: "foo" }>;
export type Foo = Values["foo"];
/**
* Foo === {
* _createdAt: string;
Expand All @@ -227,3 +227,69 @@ Typescript was an after-the-fact concern with sanity, since the rise of typescri
The long term goal is to deprecate the monorepo altogether. Building this seperately was to move quickly and these features should be in sanity directly (and is likely one of their internal goals). The idea is to introduce these changes iteratively into sanity itself while removing them from this library, until it's reduced to simply passing through the `define*` methods directly, and will then be deprecated.

This shouldn't deter you from using it! Under the hood, it's passing all the inputs to sanity's native `define*` methods, so you shouldn't have any runtime differences. With all the typings being attempting to make their way into sanity, you should keep all the benefits of just importing the `define*` methods and noticing no differences.

## Migrations

### Migrating from 2.x to 3.x

#### InferSchemaValues

`InferSchemaValues<typeof config>` used to return a union of all types but now returns an object keyed off by type. This is because using `Extract` to retrieve specific type was difficult. Object types would have a `_type` for easy extraction, but all the other types were less reliable (i.e. arrays and primitives).

```diff
export default config;

type Values = InferSchemaValues<typeof config>;

- export type Product = Extract<Values, { _type: "product" }>
+ export type Product = Values["product"];
```

#### InferValue

Types used to be inferred using `InferValue<typeof type>` for easy exporting. Now, `InferSchemaValues<typeof config>` needs to be used, and individual types keyed off of it. The reason for this is that only the config has context about aliased types, so `InferValue` was always going to be missing those values.

```diff
const product = defineType({
name: "product",
type: "document",
title: "Product",
fields: [
// ...
],
});

- export type Product = InferValue<typeof product>;

const config = defineConfig({
// ...
schema: {
types: [
product,
// ...
],
},
});

export default config;

type Values = InferSchemaValues<typeof config>;

+ export type Product = Values["product"];
```

You can still use `_InferValue` but this is discouraged, because it will be missing the context from the config:

```diff
const product = defineType({
name: "product",
type: "document",
title: "Product",
fields: [
// ...
],
});

- export type Product = InferValue<typeof product>;
+ export type Product = _InferValue<typeof product>;
```
Loading

0 comments on commit f0fda21

Please sign in to comment.