typia 4.1.16 → 4.2.0-dev.20230807

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,124 +1,124 @@
1
- import ts from "typescript";
2
-
3
- export namespace TypeFactory {
4
- export const resolve =
5
- (checker: ts.TypeChecker) =>
6
- (type: ts.Type): ts.Type | null =>
7
- getReturnType(checker)(type)("toJSON");
8
-
9
- export const isFunction = (type: ts.Type): boolean =>
10
- getFunction(type) !== null;
11
-
12
- const getFunction = (type: ts.Type) => {
13
- const node = type.symbol?.declarations?.[0];
14
- if (node === undefined) return null;
15
-
16
- return ts.isFunctionLike(node)
17
- ? node
18
- : ts.isPropertyAssignment(node) || ts.isPropertyDeclaration(node)
19
- ? ts.isFunctionLike(node.initializer)
20
- ? node.initializer
21
- : null
22
- : null;
23
- };
24
-
25
- export const getReturnType =
26
- (checker: ts.TypeChecker) =>
27
- (type: ts.Type) =>
28
- (name: string): ts.Type | null => {
29
- // FIND TO-JSON METHOD
30
- const symbol: ts.Symbol | undefined = type.getProperty(name);
31
- if (!symbol) return null;
32
- else if (!symbol.valueDeclaration) return null;
33
-
34
- // GET FUNCTION DECLARATION
35
- const functor: ts.Type = checker.getTypeOfSymbolAtLocation(
36
- symbol,
37
- symbol.valueDeclaration,
38
- );
39
-
40
- // RETURNS THE RETURN-TYPE
41
- const signature: ts.Signature | undefined =
42
- checker.getSignaturesOfType(functor, ts.SignatureKind.Call)[0];
43
- return signature ? signature.getReturnType() : null;
44
- };
45
-
46
- export const getFullName =
47
- (checker: ts.TypeChecker) =>
48
- (type: ts.Type, symbol?: ts.Symbol): string => {
49
- // PRIMITIVE
50
- symbol ??= type.aliasSymbol ?? type.getSymbol();
51
- if (symbol === undefined) return checker.typeToString(type);
52
-
53
- // UNION OR INTERSECT
54
- if (
55
- type.aliasSymbol === undefined &&
56
- type.isUnionOrIntersection()
57
- ) {
58
- const joiner: string = type.isIntersection() ? " & " : " | ";
59
- return type.types
60
- .map((child) => getFullName(checker)(child))
61
- .join(joiner);
62
- }
63
-
64
- //----
65
- // SPECIALIZATION
66
- //----
67
- const name: string = get_name(symbol);
68
-
69
- // CHECK GENERIC
70
- const generic: readonly ts.Type[] = type.aliasSymbol
71
- ? type.aliasTypeArguments || []
72
- : checker.getTypeArguments(type as ts.TypeReference);
73
- return generic.length
74
- ? name === "Promise"
75
- ? getFullName(checker)(generic[0]!)
76
- : `${name}<${generic
77
- .map((child) => getFullName(checker)(child))
78
- .join(", ")}>`
79
- : name;
80
- };
81
-
82
- const explore_name =
83
- (decl: ts.Node) =>
84
- (name: string): string =>
85
- ts.isModuleBlock(decl)
86
- ? explore_name(decl.parent.parent)(
87
- `${decl.parent.name.getFullText().trim()}.${name}`,
88
- )
89
- : name;
90
-
91
- const get_name = (symbol: ts.Symbol): string => {
92
- const parent = symbol.getDeclarations()?.[0]?.parent;
93
- return parent
94
- ? explore_name(parent)(symbol.escapedName.toString())
95
- : "__type";
96
- };
97
-
98
- export const keyword = (
99
- type:
100
- | "void"
101
- | "any"
102
- | "unknown"
103
- | "boolean"
104
- | "number"
105
- | "bigint"
106
- | "string",
107
- ) => {
108
- return ts.factory.createKeywordTypeNode(
109
- type === "void"
110
- ? ts.SyntaxKind.VoidKeyword
111
- : type === "any"
112
- ? ts.SyntaxKind.AnyKeyword
113
- : type === "unknown"
114
- ? ts.SyntaxKind.UnknownKeyword
115
- : type === "boolean"
116
- ? ts.SyntaxKind.BooleanKeyword
117
- : type === "number"
118
- ? ts.SyntaxKind.NumberKeyword
119
- : type === "bigint"
120
- ? ts.SyntaxKind.BigIntKeyword
121
- : ts.SyntaxKind.StringKeyword,
122
- );
123
- };
124
- }
1
+ import ts from "typescript";
2
+
3
+ export namespace TypeFactory {
4
+ export const resolve =
5
+ (checker: ts.TypeChecker) =>
6
+ (type: ts.Type): ts.Type | null =>
7
+ getReturnType(checker)(type)("toJSON");
8
+
9
+ export const isFunction = (type: ts.Type): boolean =>
10
+ getFunction(type) !== null;
11
+
12
+ const getFunction = (type: ts.Type) => {
13
+ const node = type.symbol?.declarations?.[0];
14
+ if (node === undefined) return null;
15
+
16
+ return ts.isFunctionLike(node)
17
+ ? node
18
+ : ts.isPropertyAssignment(node) || ts.isPropertyDeclaration(node)
19
+ ? ts.isFunctionLike(node.initializer)
20
+ ? node.initializer
21
+ : null
22
+ : null;
23
+ };
24
+
25
+ export const getReturnType =
26
+ (checker: ts.TypeChecker) =>
27
+ (type: ts.Type) =>
28
+ (name: string): ts.Type | null => {
29
+ // FIND TO-JSON METHOD
30
+ const symbol: ts.Symbol | undefined = type.getProperty(name);
31
+ if (!symbol) return null;
32
+ else if (!symbol.valueDeclaration) return null;
33
+
34
+ // GET FUNCTION DECLARATION
35
+ const functor: ts.Type = checker.getTypeOfSymbolAtLocation(
36
+ symbol,
37
+ symbol.valueDeclaration,
38
+ );
39
+
40
+ // RETURNS THE RETURN-TYPE
41
+ const signature: ts.Signature | undefined =
42
+ checker.getSignaturesOfType(functor, ts.SignatureKind.Call)[0];
43
+ return signature ? signature.getReturnType() : null;
44
+ };
45
+
46
+ export const getFullName =
47
+ (checker: ts.TypeChecker) =>
48
+ (type: ts.Type, symbol?: ts.Symbol): string => {
49
+ // PRIMITIVE
50
+ symbol ??= type.aliasSymbol ?? type.getSymbol();
51
+ if (symbol === undefined) return checker.typeToString(type);
52
+
53
+ // UNION OR INTERSECT
54
+ if (
55
+ type.aliasSymbol === undefined &&
56
+ type.isUnionOrIntersection()
57
+ ) {
58
+ const joiner: string = type.isIntersection() ? " & " : " | ";
59
+ return type.types
60
+ .map((child) => getFullName(checker)(child))
61
+ .join(joiner);
62
+ }
63
+
64
+ //----
65
+ // SPECIALIZATION
66
+ //----
67
+ const name: string = get_name(symbol);
68
+
69
+ // CHECK GENERIC
70
+ const generic: readonly ts.Type[] = type.aliasSymbol
71
+ ? type.aliasTypeArguments || []
72
+ : checker.getTypeArguments(type as ts.TypeReference);
73
+ return generic.length
74
+ ? name === "Promise"
75
+ ? getFullName(checker)(generic[0]!)
76
+ : `${name}<${generic
77
+ .map((child) => getFullName(checker)(child))
78
+ .join(", ")}>`
79
+ : name;
80
+ };
81
+
82
+ const explore_name =
83
+ (decl: ts.Node) =>
84
+ (name: string): string =>
85
+ ts.isModuleBlock(decl)
86
+ ? explore_name(decl.parent.parent)(
87
+ `${decl.parent.name.getFullText().trim()}.${name}`,
88
+ )
89
+ : name;
90
+
91
+ const get_name = (symbol: ts.Symbol): string => {
92
+ const parent = symbol.getDeclarations()?.[0]?.parent;
93
+ return parent
94
+ ? explore_name(parent)(symbol.escapedName.toString())
95
+ : "__type";
96
+ };
97
+
98
+ export const keyword = (
99
+ type:
100
+ | "void"
101
+ | "any"
102
+ | "unknown"
103
+ | "boolean"
104
+ | "number"
105
+ | "bigint"
106
+ | "string",
107
+ ) => {
108
+ return ts.factory.createKeywordTypeNode(
109
+ type === "void"
110
+ ? ts.SyntaxKind.VoidKeyword
111
+ : type === "any"
112
+ ? ts.SyntaxKind.AnyKeyword
113
+ : type === "unknown"
114
+ ? ts.SyntaxKind.UnknownKeyword
115
+ : type === "boolean"
116
+ ? ts.SyntaxKind.BooleanKeyword
117
+ : type === "number"
118
+ ? ts.SyntaxKind.NumberKeyword
119
+ : type === "bigint"
120
+ ? ts.SyntaxKind.BigIntKeyword
121
+ : ts.SyntaxKind.StringKeyword,
122
+ );
123
+ };
124
+ }