Skip to content

Commit

Permalink
make derived type more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Feb 12, 2018
1 parent 1d8a9de commit 7b4525c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ export interface Props {
export const type = <P extends Props>(
props: P,
name: string = getNameFromProps(props)
): InterfaceType<P, TypeOfProps<P>, OutputOfProps<P>, mixed> =>
): InterfaceType<P, { [K in keyof P]: TypeOf<P[K]> }, { [K in keyof P]: OutputOf<P[K]> }, mixed> =>
new InterfaceType(
name,
(m): m is TypeOfProps<P> => {
Expand Down Expand Up @@ -550,7 +550,7 @@ export type InputOfPartialProps<P extends AnyProps> = { [K in keyof P]?: OutputO
export const partial = <P extends Props>(
props: P,
name: string = `PartialType<${getNameFromProps(props)}>`
): PartialType<P, TypeOfPartialProps<P>, InputOfPartialProps<P>, mixed> => {
): PartialType<P, { [K in keyof P]?: TypeOf<P[K]> }, { [K in keyof P]?: OutputOf<P[K]> }, mixed> => {
const partials: Props = {}
for (let k in props) {
partials[k] = union([props[k], undefinedType])
Expand Down Expand Up @@ -602,7 +602,7 @@ export const dictionary = <D extends Mixed, C extends Mixed>(
domain: D,
codomain: C,
name: string = `{ [K in ${domain.name}]: ${codomain.name} }`
): DictionaryType<D, C, TypeOfDictionary<D, C>, OutputOfDictionary<C, D>, mixed> =>
): DictionaryType<D, C, { [K in TypeOf<D>]: TypeOf<C> }, { [K in OutputOf<D>]: OutputOf<C> }, mixed> =>
new DictionaryType(
name,
(m): m is TypeOfDictionary<D, C> =>
Expand Down Expand Up @@ -955,7 +955,7 @@ export class StrictType<P extends AnyProps, A = any, O = A, I = mixed> extends T
export const strict = <P extends Props>(
props: P,
name: string = `StrictType<${getNameFromProps(props)}>`
): StrictType<P, TypeOfProps<P>, OutputOfProps<P>, mixed> => {
): StrictType<P, { [K in keyof P]: TypeOf<P[K]> }, { [K in keyof P]: OutputOf<P[K]> }, mixed> => {
const loose = type(props)
return new StrictType(
name,
Expand Down
4 changes: 2 additions & 2 deletions typings-checker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const x11: I2T = { name: 'name', father: { surname: 'surname' } }
const D1 = t.dictionary(t.keyof({ a: true }), t.number)
// $ExpectError Type 'string' is not assignable to type 'number'
const x12: TypeOf<typeof D1> = { a: 's' }
// $ExpectError Type '{ c: number; }' is not assignable to type 'TypeOfDictionary<KeyofType<{ a: boolean; }>, NumberType>'
// $ExpectError Type '{ c: number; }' is not assignable to type '{ a: number; }'.
const x12_2: TypeOf<typeof D1> = { c: 1 }
const x13: TypeOf<typeof D1> = { a: 1 }

Expand Down Expand Up @@ -306,7 +306,7 @@ f(Rec) // OK!
// tagged union
//
const TU1 = t.taggedUnion('type', [t.type({ type: t.literal('a') }), t.type({ type: t.literal('b') })])
// $ExpectError Type 'true' is not assignable to type 'TypeOfProps<{ type: LiteralType<"a">; }> | TypeOfProps<{ type: LiteralType<"b">; }>'
// $ExpectError Type 'true' is not assignable to type '{ type: "a"; } | { type: "b"; }'
const x36: TypeOf<typeof TU1> = true
const x37: TypeOf<typeof TU1> = { type: 'a' }
const x38: TypeOf<typeof TU1> = { type: 'b' }
Expand Down

0 comments on commit 7b4525c

Please sign in to comment.