Skip to content

Commit

Permalink
types: correctly handle json objects
Browse files Browse the repository at this point in the history
  • Loading branch information
DTrombett committed May 19, 2023
1 parent 0c73a4e commit c31b0c7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"main": "./dist/index.js",
"types": "./src/index.ts",
"scripts": {
"build": "tsup",
"build": "tsup && tsc --declaration --emitDeclarationOnly --declarationDir dist --noEmit false",
"lint": "eslint src --fix",
"prettier": "prettier --write src/**/*.ts",
"start": "npm run build && node --trace-warnings --enable-source-maps .",
"test": "eslint src && tsc --noEmit && tsup _test.ts --format esm && node dist/_test.mjs",
"test": "eslint src && tsc && tsup _test.ts --format esm && node dist/_test.mjs",
"postinstall": "npm run build"
},
"repository": {
Expand Down
10 changes: 8 additions & 2 deletions src/structures/Tassa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ export class Tassa extends Base<TassaData> {
patch(data: Data) {
if (this.isJson(data)) {
this.handleJson(data);
this.dataCreazione = data.dataCreazione && new Date(data.dataCreazione);
this.dataPagamento = data.dataPagamento && new Date(data.dataPagamento);
this.dataCreazione =
data.dataCreazione === undefined
? undefined
: new Date(data.dataCreazione);
this.dataPagamento =
data.dataPagamento === undefined
? undefined
: new Date(data.dataPagamento);
this.scadenza = new Date(data.scadenza);
} else {
if (data.dataCreazione != null)
Expand Down
33 changes: 19 additions & 14 deletions src/types/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,26 +97,31 @@ export type ClientOptions = Partial<
} | null;
}
>;
export type Jsonify<T, N extends boolean = false, D extends boolean = true> = [
T,
D
] extends [
{
toJSON(): infer J;
},
true
]
? Jsonify<J, false, false>
export type Jsonify<T, D extends boolean = true> = D extends true
? Jsonify<
T extends {
toJSON(): infer J;
}
? J
: T,
false
>
: T extends boolean | number | string | null
? T
: T extends bigint
? never
: T extends symbol | ((...args: any[]) => any) | undefined
? N extends true
? never
: undefined
? undefined
: T extends (infer A)[]
? Jsonify<A>[]
: {
[K in keyof T]: Jsonify<T[K], true>;
[K in keyof T as T[K] extends
| bigint
| symbol
| ((...args: any[]) => any)
| undefined
? never
: K]: Jsonify<T[K]>;
};
export type LoginLink = {
url: string;
Expand Down
7 changes: 4 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
"include": ["src/**/*"],
"extends": "@tsconfig/node18/tsconfig.json",
"compilerOptions": {
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"allowUnreachableCode": false,
"alwaysStrict": true,
"esModuleInterop": true,
"isolatedModules": true,
"module": "ESNext",
"moduleResolution": "node",
"noEmit": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
Expand All @@ -17,7 +19,6 @@
"outDir": "dist",
"pretty": true,
"resolveJsonModule": true,
"sourceMap": true,
"module": "ESNext"
"sourceMap": true
}
}
1 change: 0 additions & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const options: Options = {
minify: true,
keepNames: true,
sourcemap: true,
dts: true,
};

export default defineConfig(options);

0 comments on commit c31b0c7

Please sign in to comment.