Skip to content

Commit 3a1481d

Browse files
feat: do not default to the any type when a type is incorrect, missing or invalid
Implicitly making things "any" made our types Worse and there was no need for it, this PR makes it so that invalid types throw errors instead of falling back to any.
1 parent 9ef6fed commit 3a1481d

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/utils.ts

+19-15
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,21 @@ export const typify = (
107107
return arrayType;
108108
}
109109

110-
if (!type) return 'any';
110+
if (!type)
111+
throw new Error('Missing type provided to typify, something is wrong in the documentation');
111112

112113
let innerTypes: TypeInformation[] | undefined;
113114
let typeAsString: string | TypeInformation | TypeInformation[] = type;
114115

115116
if (typeof type === 'object') {
116-
let newType = type.type || 'any';
117+
if (!type.type) {
118+
console.error(type);
119+
throw new Error(
120+
'Missing type property on object provided to typify, something is wrong in the documentation',
121+
);
122+
}
123+
124+
let newType = type.type;
117125

118126
if (typeof newType === 'string' && newType.toLowerCase() === 'string') {
119127
const stringType = type as DetailedStringType;
@@ -167,21 +175,19 @@ export const typify = (
167175
return 'number[]';
168176
case 'array': {
169177
if (innerTypes) return `Array<${typify(innerTypes[0])}>`;
170-
debug(chalk.yellow('Untyped "Array" as return type'));
171-
return 'any[]';
178+
throw new Error('Untyped "Array" as return type');
172179
}
173180
case 'true':
174181
case 'false':
175-
debug(chalk.cyan('"true" or "false" provided as return value, inferring "Boolean" type'));
176-
return 'boolean';
182+
throw new Error('"true" or "false" provided as return value, inferring "Boolean" type');
177183
case '[objects]':
178-
debug(
179-
chalk.red('[Objects] is not a valid array definition, please conform to the styleguide'),
184+
throw new Error(
185+
'[Objects] is not a valid array definition, please conform to the styleguide',
180186
);
181-
return 'any[]';
182187
case 'object':
183-
debug(chalk.yellow('Unstructured "Object" type specified'));
184-
return 'any';
188+
throw new Error(
189+
'Unstructured "Object" type specified, you must specify either the type of the object or provide the key structure inline in the documentation',
190+
);
185191
case 'any':
186192
return 'any';
187193
case 'string':
@@ -201,14 +207,12 @@ export const typify = (
201207
if (innerTypes) {
202208
return `Promise<${prefixTypeForSafety(typify(innerTypes[0]))}>`;
203209
}
204-
debug(chalk.red('Promise with missing inner type, defaulting to any'));
205-
return 'Promise<any>';
210+
throw new Error('Promise with missing inner type');
206211
case 'record':
207212
if (innerTypes && innerTypes.length === 2) {
208213
return `Record<${typify(innerTypes[0])}, ${typify(innerTypes[1])}>`;
209214
}
210-
debug(chalk.red('Record with missing inner types, default to any'));
211-
return 'Record<any, any>';
215+
throw new Error('Record with missing inner types');
212216
case 'partial':
213217
if (!innerTypes || innerTypes.length !== 1) {
214218
throw new Error('Partial generic type must have exactly one inner type. i.e. Partial<T>');

0 commit comments

Comments
 (0)