Skip to content

Commit

Permalink
fix: typescript 2.9
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed May 12, 2020
1 parent 1c11563 commit edea75d
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 97 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
"@commitlint/config-conventional": "^8.3.4",
"@types/jest": "^25.2.1",
"@types/node": "^13.13.5",
"@typescript-eslint/eslint-plugin": "^2.31.0",
"@typescript-eslint/parser": "^2.31.0",
"@typescript-eslint/eslint-plugin": "^2.33.0",
"@typescript-eslint/parser": "^2.33.0",
"cross-env": "^7.0.2",
"eslint": "^6.8.0",
"eslint": "^7.0.0",
"husky": "^4.2.5",
"jest": "^26.0.1",
"lerna": "^3.20.2",
"ts-jest": "^25.5.0",
"typescript": "^3.8.3"
"ts-jest": "^25.5.1",
"typescript": "^3.9.2"
},
"resolutions": {
"minimist": "1.2.5"
Expand Down
8 changes: 4 additions & 4 deletions packages/unit/src/Context/Context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isObject, Logger} from "@typesafeunit/util";
import {isInstanceOf, Logger} from "@typesafeunit/util";
import {Service} from "../Service";
import {ApplyContext, ResolveService} from "./interfaces";

Expand All @@ -7,12 +7,12 @@ const weakCache = new WeakMap();
export class Context {
public static logger = Logger.factory(Context);

public static async resolve<T extends any>(value: T): Promise<ResolveService<T>> {
if (isObject(value) && Object.getPrototypeOf(value) instanceof Service) {
public static async resolve<T extends Service<any>>(value: T): Promise<ResolveService<T>> {
if (isInstanceOf<Service<T>>(value, Service)) {
return value.resolve();
}

return value as any;
return value;
}

public static async apply<C extends Context>(context: C): Promise<ApplyContext<C>> {
Expand Down
10 changes: 6 additions & 4 deletions packages/unit/src/Context/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ export type ApplyContext<C extends Context> = {
: C[K];
};

export type Ex<C, K> = K extends keyof C ? C[K] : never;

export type MatchContext<C extends any, T> = {
[K in keyof T]: C[K] extends T[K]
[K in keyof T]: Ex<C, K> extends T[K]
? T[K]
: C[K] extends IServiceResolver<infer M>
: Ex<C, K> extends IServiceResolver<infer M>
? M extends T[K]
? T[K] : ["The IServiceResolver<T> doesn't match context", C[K], T[K]]
: ["The input type doesn't match context", C[K], T[K]];
? T[K] : ["The IServiceResolver<T> doesn't match context", Ex<C, K>, T[K]]
: ["The input type doesn't match context", Ex<C, K>, T[K]];
};
4 changes: 4 additions & 0 deletions packages/util/src/Date/DateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export class DateTime {
assert(this.#date.getTime() > 0);
}

public static from(date: string | number | Date = new Date()) {
return new this(date);
}

public get date() {
return new Date(this.#date);
}
Expand Down
15 changes: 12 additions & 3 deletions packages/util/src/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ export const isFunction = (value: any): value is (...args: any[]) => any => {
return typeof value === "function" && isUndefined(value.prototype);
};

export type Ctor<T, A extends any[] = any[]> = {
new(...args: A): T;
prototype: T;
};

export type InstanceType<T, A extends any[] = any[]> = Function & { prototype: T };

export type Ctor<T = any, A extends any[] = any[]> = { new(...args: A): T };
export const isInstanceOf = <T>(value: any, type: {new(): T}): value is T => value instanceof type;
export const isObject = (value: any): value is object => !isNull(value) && typeof value === "object";

export const isObject = <T extends object>(value: any): value is T => typeof value === "object" && !isNull(value);
export const isClass = (value: any): value is () => any => typeof value === "function" && isObject(value.prototype);
export const isError = (value: any): value is Error => typeof value === "object" && value instanceof Error;

export function isInstanceOf<T extends any>(value: any, type: InstanceType<T>): value is T {
return isObject(value) && value instanceof type;
}
Loading

0 comments on commit edea75d

Please sign in to comment.