Skip to content

Commit

Permalink
feat: Removed support for Alias & removed Auto (#14)
Browse files Browse the repository at this point in the history
* Simplified (removed Auto, Alias, alias support)

* Rename

* Import fixes + playbook reset

* tsconfig fix

* Minor edits to the README

* Workflows update

* Adds license

* Adds keywords

* Rename
  • Loading branch information
shoooe committed Mar 8, 2023
1 parent 5cefbad commit 6de679d
Show file tree
Hide file tree
Showing 39 changed files with 188 additions and 549 deletions.
2 changes: 0 additions & 2 deletions .github/pull-request-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

QA should verify that the following things hold true (see `qa/playbook.ts`):

- [ ] Autocomplete works on `Shape`
- [ ] Autocomplete works on `Derive`
- [ ] Autocomplete works for keys on `Alias`
8 changes: 4 additions & 4 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Setup node.js
uses: actions/setup-node@v1
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '16'
node-version: 'lts/*'

- name: Install dependencies
run: yarn install
run: yarn install --frozen-lockfile

- name: Run linter
run: yarn lint
6 changes: 2 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ jobs:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 'lts/*'

- name: Install dependencies
run: yarn install --frozen-lockfile

Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Setup node.js
uses: actions/setup-node@v1
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '16'
node-version: 'lts/*'

- name: Install dependencies
run: yarn install
run: yarn install --frozen-lockfile

- name: Run tests
run: yarn test
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
The MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
81 changes: 14 additions & 67 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

[![npm version](https://badge.fury.io/js/@shoooe%2Fderive.svg)](https://badge.fury.io/js/@shoooe%2Fderive)

Utility type to generate a type starting from another.
Utility type to generate a type from another with a special care for the DX.

You can see this tool as an hardcode version of `Pick`.
You can see this tool as an _hardcode_ version of `Pick`.

Features:

- 😎 Type safe
- 🌱 Minimal & lightweight
- ⌨️​ Autocompletion for fields
- 💥 Automatically expands arrays and nullable/optional types
- 👀 Preview expanded types in intellisense
- 💫 Supports recursive & mutually recursive types
- ❓ Optional fields support
- 💋 Inspired by GraphQL
- 🛠 Supports [GraphQL Code Generator](https://github.com/dotansimha/graphql-code-generator) types

## Installation

Expand Down Expand Up @@ -53,29 +53,27 @@ We can derive a subset of its properties via:
type Result = Derive<
User,
{
// `Auto` = infer original type (`number`)
id: Auto;

// `Auto` = infer original type (`string`)
name: Auto;
id: true;
name: true;

// Automatically expands nullable & optional types, which means that `null`
// and `undefined` will be added automatically to the resulting type if
// they existed in the target type.
bestFriend: {
name: Auto;
name: true;
};

// Automatically expands arrays as well
friends: {
name: Auto;
name: true;

// Supports mutually recursive types
favoriteBook: {
isdn: Auto;
title: Auto;
synopsis: Auto;
isdn: true;
title: true;
synopsis: true;
author: {
name: Auto;
name: true;
};
};
};
Expand Down Expand Up @@ -110,60 +108,9 @@ type Result = {
};
```

### Reusing shapes

You can extract and reuse shapes:

```typescript
type CustomShape = Shape<
User,
{
id: Auto;
name: Auto;
bestFriend: {
name: Auto;
};
}
>;
```

Once defined you can use them directly to generate the type:

```typescript
type Result = Derive<User, CustomShape>;
```

Otherwise you can use them inside other shapes:

```typescript
type Result = Derive<
User,
{
id: Auto;
name: Auto;
bestFriend: CustomShape;
}
>;
```

### Aliases

You can alias a field from another type and infer nullability and optionality using `Alias` like this:
## Alternatives

```typescript
type Result = Derive<
User,
{
alias: Alias<Book, 'subtitle', Auto>;
}
>;
```

which will result in:

```typescript
type Result = { alias?: string | null };
```
- [ts-essentials](https://github.com/ts-essentials/ts-essentials): comprehensive library with a different style for `DeepPick` (it doesn't do automatic expansion)

## Credits

Expand Down
5 changes: 1 addition & 4 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
export type { Derive } from './src/Derive';
export type { Auto } from './src/Auto';
export type { Shape } from './src/Shape';
export type { Alias } from './src/Alias';
export type { Derive } from './src/derive';
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"keywords": [
"safe",
"derive",
"pick",
"deep",
"types",
"typesafe",
"typescript",
Expand Down
34 changes: 2 additions & 32 deletions qa/playbook.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,17 @@
import { Alias } from '../src/Alias';
import { Auto } from '../src/Auto';
import { Derive } from '../src/Derive';
import { Shape } from '../src/Shape';
import { Derive } from '../src/derive';

type User = {
id: string;
name?: string;
bestFriend: User | null;
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type ShapeAutocomplete = Shape<
User,
{
id: Auto;
// CHECK: start typing "name" and notice the autocomplete
// CHECK: start typing "bestFriend" and then "name" and notice the autocomplete for the nested type
}
>;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type DeriveAutocomplete = Derive<
User,
{
id: Auto;
id: true;
// CHECK: start typing "name" and notice the autocomplete
// CHECK: start typing "bestFriend" and then "name" and notice the autocomplete for the nested type
}
>;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type AliasAutocomplete = Shape<
User,
{
id: Auto;
alias: Alias<User, 'id', Auto>; // CHECK: remove 'id' and notice the autocomplete for keys
nestedAlias: Alias<
User,
'bestFriend',
{
id: Auto;
// CHECK: start typing "name" and notice the autocomplete
}
>;
}
>;
17 changes: 0 additions & 17 deletions src/Alias.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/Auto.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/EmptyRecord.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/Shape.ts

This file was deleted.

8 changes: 3 additions & 5 deletions src/AutocompleteHelper.ts → src/autocomplete-helper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Alias } from './Alias';
import { Auto } from './Auto';
import { CoreTypeOf } from './CoreTypeOf';
import { RecordLike } from './RecordLike';
import { CoreTypeOf } from './core-type-of';
import { RecordLike } from './record-like';

/**
* Represents the possible shape values for a given target type fields.
Expand All @@ -18,4 +16,4 @@ export type AutocompleteHelper<Target> = CoreTypeOf<Target> extends RecordLike
CoreTypeOf<Target>[Key]
>;
}
: Auto | Alias<any, any, any>;
: true;
2 changes: 1 addition & 1 deletion src/CoreTypeOf.ts → src/core-type-of.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ElementTypeOf } from './ElementTypeOf';
import { ElementTypeOf } from './element-type-of';

/**
* Gets the core type of another type by stripping array notations, `null` and
Expand Down
Loading

0 comments on commit 6de679d

Please sign in to comment.