-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from airtoxin/interface
Add Interface type
- Loading branch information
Showing
4 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Interface } from "./Interface"; | ||
import { GetType, optional, Right, string } from "purify-ts"; | ||
|
||
describe("Interface", () => { | ||
const c = Interface({ str: string, ops: optional(string) }); | ||
|
||
it("should return interface Codec", () => { | ||
expect(c.decode({ str: "foo" })).toEqual(Right({ str: "foo" })); | ||
// compile test | ||
const v: GetType<typeof c> = { str: "foo" }; | ||
expect(v).toEqual({ str: "foo" }); | ||
}); | ||
|
||
it("should return the type accepting object that its optional field to be not defined", () => { | ||
// compile test: 'ops' field not defined but acceptable | ||
// `GetType<typeof c>` equals to `{ str: string, ops?: string | undefined }` | ||
const v: GetType<typeof c> = { str: "foo" }; | ||
expect(v).toEqual({ str: "foo" }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Codec, GetType } from "purify-ts"; | ||
|
||
export const Interface = <T extends Record<string, Codec<any>>>( | ||
properties: T | ||
): Codec< | ||
{ | ||
[K in keyof ({ | ||
[K in keyof Pick< | ||
T, | ||
{ | ||
[K in keyof T]-?: undefined extends GetType<T[K]> ? never : K; | ||
}[keyof T] | ||
>]: GetType<T[K]>; | ||
} & | ||
{ | ||
[K in keyof Pick< | ||
T, | ||
{ | ||
[K in keyof T]-?: undefined extends GetType<T[K]> ? K : never; | ||
}[keyof T] | ||
>]?: Exclude<GetType<T[K]>, undefined>; | ||
})]: GetType<T[K]>; | ||
} | ||
> => Codec.interface(properties) as any; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./sequenceWeak"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./Codec"; | ||
export * from "./Either"; |