Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow creating tuples with unlimited types #441

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 deletions docs/modules/index.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -666,31 +666,33 @@ Added in v1.5.3
**Signature**

```ts
export interface TupleC<CS extends [Mixed, ...Array<Mixed>]>
export interface TupleC<CS extends readonly [Mixed, ...Array<Mixed>]>
extends TupleType<
CS,
CS extends { length: 1 }
CS extends { readonly length: 1 }
? [TypeOf<CS[0]>]
: CS extends { length: 2 }
: CS extends { readonly length: 2 }
? [TypeOf<CS[0]>, TypeOf<CS[1]>]
: CS extends { length: 3 }
: CS extends { readonly length: 3 }
? [TypeOf<CS[0]>, TypeOf<CS[1]>, TypeOf<CS[2]>]
: CS extends { length: 4 }
: CS extends { readonly length: 4 }
? [TypeOf<CS[0]>, TypeOf<CS[1]>, TypeOf<CS[2]>, TypeOf<CS[3]>]
: CS extends { length: 5 }
: CS extends { readonly length: 5 }
? [TypeOf<CS[0]>, TypeOf<CS[1]>, TypeOf<CS[2]>, TypeOf<CS[3]>, TypeOf<CS[4]>]
: unknown,
CS extends { length: 1 }
// : { [K in keyof CS]: CS[K] extends Mixed ? TypeOf<CS[K]> : unknown },
CS extends { readonly length: 1 }
? [OutputOf<CS[0]>]
: CS extends { length: 2 }
: CS extends { readonly length: 2 }
? [OutputOf<CS[0]>, OutputOf<CS[1]>]
: CS extends { length: 3 }
: CS extends { readonly length: 3 }
? [OutputOf<CS[0]>, OutputOf<CS[1]>, OutputOf<CS[2]>]
: CS extends { length: 4 }
: CS extends { readonly length: 4 }
? [OutputOf<CS[0]>, OutputOf<CS[1]>, OutputOf<CS[2]>, OutputOf<CS[3]>]
: CS extends { length: 5 }
: CS extends { readonly length: 5 }
? [OutputOf<CS[0]>, OutputOf<CS[1]>, OutputOf<CS[2]>, OutputOf<CS[3]>, OutputOf<CS[4]>]
: unknown,
// : { [K in keyof CS]: CS[K] extends Mixed ? OutputOf<CS[K]> : unknown },
unknown
> {}
```
Expand Down Expand Up @@ -2259,20 +2261,10 @@ Added in v1.0.0
**Signature**

```ts
export declare function tuple<A extends Mixed, B extends Mixed, C extends Mixed, D extends Mixed, E extends Mixed>(
codecs: [A, B, C, D, E],
name?: string
): TupleC<[A, B, C, D, E]>
export declare function tuple<A extends Mixed, B extends Mixed, C extends Mixed, D extends Mixed>(
codecs: [A, B, C, D],
name?: string
): TupleC<[A, B, C, D]>
export declare function tuple<A extends Mixed, B extends Mixed, C extends Mixed>(
codecs: [A, B, C],
name?: string
): TupleC<[A, B, C]>
export declare function tuple<A extends Mixed, B extends Mixed>(codecs: [A, B], name?: string): TupleC<[A, B]>
export declare function tuple<A extends Mixed>(codecs: [A], name?: string): TupleC<[A]>
export declare function tuple<CS extends readonly [Mixed, ...Array<Mixed>]>(
codecs: CS,
name: string = `[${codecs.map((type) => type.name).join(', ')}]`
): TupleC<CS>
```

Added in v1.0.0
Expand Down
1 change: 1 addition & 0 deletions dtslint/after3.8/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TypeScript Version: 3.8
File renamed without changes.
9 changes: 9 additions & 0 deletions dtslint/after3.8/ts3.8/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as t from '../../../src'

const Tuple8 = t.tuple([t.string, t.string, t.string, t.string, t.string, t.string])
type Tuple8TypeTest = t.TypeOf<typeof Tuple8> // $ExpectType [string, string, string, string, string, string]
type Tuple8OutputTest = t.OutputOf<typeof Tuple8> // $ExpectType [string, string, string, string, string, string]

const Tuple9 = t.tuple([t.string, t.string, t.boolean, t.string, t.string, t.string, t.string])
type Tuple9TypeTest = t.TypeOf<typeof Tuple9> // $ExpectType [string, string, boolean, string, string, string, string]
type Tuple9OutputTest = t.OutputOf<typeof Tuple9> // $ExpectType [string, string, boolean, string, string, string, string]
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file added dtslint/base/ts3.5/index.d.ts
Empty file.
5 changes: 1 addition & 4 deletions dtslint/ts3.5/index.ts → dtslint/base/ts3.5/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as t from '../../src'
import * as t from '../../../src'
import { either } from 'fp-ts/lib/Either'

//
Expand Down Expand Up @@ -239,9 +239,6 @@ const Tuple7 = t.tuple([t.string, t.number, t.boolean, t.null, t.undefined]) //
type Tuple7TypeTest = t.TypeOf<typeof Tuple7> // $ExpectType [string, number, boolean, null, undefined]
type Tuple7OutputTest = t.OutputOf<typeof Tuple7> // $ExpectType [string, number, boolean, null, undefined]

// $ExpectError
const Tuple8 = t.tuple([t.string, t.string, t.string, t.string, t.string, t.string])

//
// partial
//
Expand Down
16 changes: 16 additions & 0 deletions dtslint/base/ts3.5/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"noEmit": true,
"strict": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitReturns": false,
"noUnusedLocals": false,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"target": "es5",
"lib": ["es2015"]
}
}
19 changes: 19 additions & 0 deletions dtslint/base/ts3.5/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"semicolon": false,
"array-type": false,
"no-unnecessary-generics": false,
"member-access": false,
"no-empty-interface": false,
"no-arg": false,
"no-object-literal-type-assertion": false,
"no-unnecessary-class": false,
"radix": false,
"no-angle-bracket-type-assertion": false,
"object-literal-shorthand": false,
"prefer-object-spread": false,
"whitespace": false,
"use-default-type-parameter": false
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"build": "npm run clean && tsc && tsc -p tsconfig.es6.json && npm run import-path-rewrite",
"prepublish": "npm run build",
"perf": "ts-node perf/index",
"dtslint": "dtslint dtslint",
"dtslint": "dtslint dtslint/base && dtslint dtslint/after3.8",
"declaration": "tsc -p declaration/tsconfig.json",
"mocha": "TS_NODE_CACHE=false mocha -r ts-node/register test/*.ts",
"doctoc": "doctoc README.md",
Expand Down
46 changes: 16 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ const getNameFromProps = (props: Props): string =>
.map((k) => `${k}: ${props[k].name}`)
.join(', ')

const useIdentity = (codecs: Array<Any>): boolean => {
const useIdentity = (codecs: readonly Any[]): boolean => {
for (let i = 0; i < codecs.length; i++) {
if (codecs[i].encode !== identity) {
return false
Expand Down Expand Up @@ -1431,7 +1431,7 @@ export function intersection<CS extends [Mixed, Mixed, ...Array<Mixed>]>(
/**
* @since 1.0.0
*/
export class TupleType<CS extends Array<Any>, A = any, O = A, I = unknown> extends Type<A, O, I> {
export class TupleType<CS extends readonly Any[], A = any, O = A, I = unknown> extends Type<A, O, I> {
/**
* @since 1.0.0
*/
Expand All @@ -1450,52 +1450,38 @@ export class TupleType<CS extends Array<Any>, A = any, O = A, I = unknown> exten
/**
* @since 1.5.3
*/
export interface TupleC<CS extends [Mixed, ...Array<Mixed>]>
export interface TupleC<CS extends readonly [Mixed, ...Array<Mixed>]>
extends TupleType<
CS,
CS extends { length: 1 }
CS extends { readonly length: 1 }
? [TypeOf<CS[0]>]
: CS extends { length: 2 }
: CS extends { readonly length: 2 }
? [TypeOf<CS[0]>, TypeOf<CS[1]>]
: CS extends { length: 3 }
: CS extends { readonly length: 3 }
? [TypeOf<CS[0]>, TypeOf<CS[1]>, TypeOf<CS[2]>]
: CS extends { length: 4 }
: CS extends { readonly length: 4 }
? [TypeOf<CS[0]>, TypeOf<CS[1]>, TypeOf<CS[2]>, TypeOf<CS[3]>]
: CS extends { length: 5 }
: CS extends { readonly length: 5 }
? [TypeOf<CS[0]>, TypeOf<CS[1]>, TypeOf<CS[2]>, TypeOf<CS[3]>, TypeOf<CS[4]>]
: unknown,
CS extends { length: 1 }
: { [K in keyof CS]: CS[K] extends Mixed ? TypeOf<CS[K]> : unknown },
CS extends { readonly length: 1 }
? [OutputOf<CS[0]>]
: CS extends { length: 2 }
: CS extends { readonly length: 2 }
? [OutputOf<CS[0]>, OutputOf<CS[1]>]
: CS extends { length: 3 }
: CS extends { readonly length: 3 }
? [OutputOf<CS[0]>, OutputOf<CS[1]>, OutputOf<CS[2]>]
: CS extends { length: 4 }
: CS extends { readonly length: 4 }
? [OutputOf<CS[0]>, OutputOf<CS[1]>, OutputOf<CS[2]>, OutputOf<CS[3]>]
: CS extends { length: 5 }
: CS extends { readonly length: 5 }
? [OutputOf<CS[0]>, OutputOf<CS[1]>, OutputOf<CS[2]>, OutputOf<CS[3]>, OutputOf<CS[4]>]
: unknown,
: { [K in keyof CS]: CS[K] extends Mixed ? OutputOf<CS[K]> : unknown },
unknown
> {}

/**
* @since 1.0.0
*/
export function tuple<A extends Mixed, B extends Mixed, C extends Mixed, D extends Mixed, E extends Mixed>(
codecs: [A, B, C, D, E],
name?: string
): TupleC<[A, B, C, D, E]>
export function tuple<A extends Mixed, B extends Mixed, C extends Mixed, D extends Mixed>(
codecs: [A, B, C, D],
name?: string
): TupleC<[A, B, C, D]>
export function tuple<A extends Mixed, B extends Mixed, C extends Mixed>(
codecs: [A, B, C],
name?: string
): TupleC<[A, B, C]>
export function tuple<A extends Mixed, B extends Mixed>(codecs: [A, B], name?: string): TupleC<[A, B]>
export function tuple<A extends Mixed>(codecs: [A], name?: string): TupleC<[A]>
export function tuple<CS extends [Mixed, ...Array<Mixed>]>(
export function tuple<CS extends readonly [Mixed, ...Array<Mixed>]>(
codecs: CS,
name: string = `[${codecs.map((type) => type.name).join(', ')}]`
): TupleC<CS> {
Expand Down