Skip to content

Commit

Permalink
Fix type accessors (#38)
Browse files Browse the repository at this point in the history
* few comments

* rework the types (fixes 34)

* add package.json to .prettierignore and fix package.json layout changes

* remove package.json from .ptettierignore

* remove last line from .ptettierignore
  • Loading branch information
sledorze authored and pelotom committed May 10, 2018
1 parent 9be48a4 commit 479f98d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lib
coverage
package.json
README.md
README.md
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"types": "./lib/index.d.ts",
"scripts": {
"build": "tsc --pretty",
"format": "prettier $([ \"$CI\" == true ] && echo --list-different || echo --write) './**/*.{ts,tsx,js,json,css}'",
"format":
"prettier $([ \"$CI\" == true ] && echo --list-different || echo --write) './**/*.{ts,tsx,js,json,css}'",
"test": "jest --no-cache --verbose --coverage",
"test:watch": "jest --watch"
},
Expand Down
13 changes: 12 additions & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ofType, unionize } from '.';
import { ofType, unionize, UnionOf, Unionized } from '.';

describe('merged', () => {
const Foo = unionize({
Expand Down Expand Up @@ -347,3 +347,14 @@ describe('transform without value prop', () => {
expect(Data.transform({ str: strLen })(str)).toEqual(num);
});
});

describe('type accessors', () => {
describe('unionOf', () => {
it('should be usable', () => {
const T = unionize({
foo: ofType<{ x: number }>(),
});
type ActionType = UnionOf<typeof T>;
});
});
});
19 changes: 14 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
export type Unionized<Record, TaggedRecord, TagProp extends string> = {
export type Unionized<Record, TaggedRecord, TagProp extends string> = UnionTypes<
Record,
TaggedRecord
> &
Creators<Record, TaggedRecord, TagProp> &
UnionExtensions<Record, TaggedRecord>;

export interface UnionTypes<Record, TaggedRecord> {
_Tags: keyof TaggedRecord;
_Record: Record;
_Union: TaggedRecord[keyof TaggedRecord];
}
export interface UnionExtensions<Record, TaggedRecord> {
is: Predicates<TaggedRecord>;
as: Casts<Record, TaggedRecord[keyof TaggedRecord]>;
match: Match<Record, TaggedRecord[keyof TaggedRecord]>;
transform: Transform<Record, TaggedRecord[keyof TaggedRecord]>;
} & Creators<Record, TaggedRecord, TagProp>;
}

export type TagsOf<U extends Unionized<any, any, any>> = U['_Tags'];
export type RecordOf<U extends Unionized<any, any, any>> = U['_Record'];
export type UnionOf<U extends Unionized<any, any, any>> = U['_Union'];
export type TagsOf<U extends UnionTypes<any, any>> = U['_Tags'];
export type RecordOf<U extends UnionTypes<any, any>> = U['_Record'];
export type UnionOf<U extends UnionTypes<any, any>> = U['_Union'];

export type Creators<Record, TaggedRecord, TagProp extends string> = {
[T in keyof Record]: {} extends UnTagged<Record[T], TagProp>
Expand Down

0 comments on commit 479f98d

Please sign in to comment.