Skip to content

Commit 324730e

Browse files
committed
Fix NormalizeServerDates mangling types
1 parent 7bfbce9 commit 324730e

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

Diff for: src/index.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,10 @@ export namespace Typesaurus {
109109
> = Core.ConcatModel<ModelToConcatTo, ModelToConcat>;
110110

111111
/**
112-
* Normalizes server dates in an object. It replaces ServerDate with regular Date. It's useful when reusing interfaces
113-
* in a non-Typesaurus environment or when you need to store it in an array (where server dates are not allowed).
112+
* Deeply normalizes server dates in a given type. It replaces ServerDate with
113+
* regular Date. It's useful when reusing interfaces in a non-Typesaurus
114+
* environment or when you need to store it in an array (where server dates
115+
* are not allowed).
114116
*/
115-
export type NormalizeServerDates<Interface> =
116-
Core.NormalizeServerDates<Interface>;
117+
export type NormalizeServerDates<Type> = Core.NormalizeServerDates<Type>;
117118
}

Diff for: src/types/core.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -1530,14 +1530,20 @@ export namespace TypesaurusCore {
15301530
: never;
15311531

15321532
/**
1533-
* Normalizes server dates in an object. It replaces ServerDate with regular Date. It's useful when reusing interfaces
1534-
* in a non-Typesaurus environment or when you need to store it in an array (where server dates are not allowed).
1533+
* Deeply normalizes server dates in a given type. It replaces ServerDate with
1534+
* regular Date. It's useful when reusing interfaces in a non-Typesaurus
1535+
* environment or when you need to store it in an array (where server dates
1536+
* are not allowed).
15351537
*/
1536-
export type NormalizeServerDates<Interface> = {
1537-
[Key in keyof Interface]: Interface[Key] extends ServerDate
1538-
? Date
1539-
: NormalizeServerDates<Interface[Key]>;
1540-
};
1538+
export type NormalizeServerDates<Type> = Type extends ServerDate
1539+
? Date
1540+
: Type extends Date | Ref<any> | string | number | boolean
1541+
? Type
1542+
: Type extends Array<infer Item>
1543+
? Array<NormalizeServerDates<Item>>
1544+
: Type extends object
1545+
? { [Key in keyof Type]: NormalizeServerDates<Type[Key]> }
1546+
: never;
15411547

15421548
/**
15431549
* The options. It allows to configure the Firebase settings.

Diff for: src/tysts/core.ts

+37
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,43 @@ interface Mixed {
196196
nested2: Mixed | undefined;
197197
nested3?: Mixed;
198198
nested4?: Mixed | undefined;
199+
arrayOfObjects: Array<{
200+
date?: Date;
201+
some?: "things";
202+
things?: "some" | undefined;
203+
opaqueNumber: OpaqueNumber;
204+
opaqueString: OpaqueString;
205+
opaqueBoolean: OpaqueBoolean;
206+
}>;
199207
}
200208

201209
type OpaqueJSON = string & { __json: {} };
202210

203211
type OpaqueNumber = number & { __number: 123 };
204212

213+
type OpaqueString = string & { __string: "hello" };
214+
215+
type OpaqueBoolean = boolean & { __boolean: true };
216+
217+
interface OpaqueTypes {
218+
number: OpaqueNumber;
219+
numberArr: OpaqueNumber[];
220+
string: OpaqueString;
221+
stringArr: OpaqueString[];
222+
boolean: OpaqueBoolean;
223+
booleanArr: OpaqueBoolean[];
224+
object: {
225+
number: OpaqueNumber;
226+
string: OpaqueString;
227+
boolean: OpaqueBoolean;
228+
};
229+
array: Array<{
230+
number: OpaqueNumber;
231+
string: OpaqueString;
232+
boolean: OpaqueBoolean;
233+
}>;
234+
}
235+
205236
interface CustomCollection {
206237
hello: "world";
207238
}
@@ -3056,6 +3087,12 @@ namespace NormalizeServerDates {
30563087
>;
30573088

30583089
type Result2 = Assert<number, Core.NormalizeServerDates<number>>;
3090+
3091+
type asd = Core.NormalizeServerDates<OpaqueString>;
3092+
3093+
type Result3 = Assert<OpaqueString, Core.NormalizeServerDates<OpaqueString>>;
3094+
3095+
type Result4 = Assert<OpaqueTypes, Core.NormalizeServerDates<OpaqueTypes>>;
30593096
}
30603097

30613098
namespace Nullify {

0 commit comments

Comments
 (0)