Skip to content

Commit

Permalink
fix to work with latest typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Coquand committed May 28, 2020
1 parent 1c6951e commit 56f042c
Show file tree
Hide file tree
Showing 3 changed files with 696 additions and 953 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.1.9",
"version": "3.0.0",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down Expand Up @@ -55,6 +55,6 @@
"tsdx": "^0.13.2",
"tslib": "^1.10.0",
"typedoc": "^0.17.6",
"typescript": "^3.7.4"
"typescript": "^3.9.3"
}
}
15 changes: 9 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const isISO = (str: string): boolean =>
const isInteger = (n: number): boolean => Math.floor(n) === n && n !== Infinity;
const isStringNumber = (n: string): boolean =>
n.length !== 0 && n.match(/^[+-]?\d+(\.\d+)?$/) !== null;
type NonEmptyArray<T> = [T, ...T[]];

export class ValidationFailedError extends Error {
public error: string;
Expand Down Expand Up @@ -93,8 +92,7 @@ export class Decoder<T> {
return Result.fail(error);
});
/**
* Attempt multiple decoders in order until one succeeds. Takes a non-empty
* array of various decoders. The type signature is informally:
* Attempt multiple decoders in order until one succeeds. The type signature is informally:
* ```
* oneOf = (decoders: [Decoder<A>, Decoder<B>, ...]) => Decoder<A | B | ...>
* ```
Expand All @@ -111,9 +109,14 @@ export class Decoder<T> {
* ```
*/
public static oneOf = <T extends Decoder<any>>(
decoders: NonEmptyArray<T>
): Decoder<T extends Decoder<infer R> ? R : never> =>
Decoder.createOneOf(decoders);
decoders: T[]
): Decoder<T extends Decoder<infer R> ? R : never> => {
if (decoders.length === 0)
return (Decoder.ok as unknown) as Decoder<
T extends Decoder<infer R> ? R : never
>;
return Decoder.createOneOf(decoders);
};

private constructor(decoder: (data: any) => Result<T, DecodeError>) {
this.decoder = decoder;
Expand Down
Loading

0 comments on commit 56f042c

Please sign in to comment.