-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expect to use @throws in lib/*.d.ts to mark which methods may throw err #43528
Comments
I like this idea, but it seems much more useful if this information could be propagated by typescript, so that a function which calls something annotated with |
Something like typescript-eslint/no-floating-promises would be interesting. In this rule unhandled promises shows an error on vscode. |
Throw types are the one thing I still find myself wanting in TypeScript. Sometimes the types can get a little unwieldy, but there are very few things these days that you just outright can't express in the latest TypeScript versions. The types might be complicated, but almost anything is possible. Really makes the lack of throw/catch typings feel like a pretty big hole.
|
I manage a Typescript app that is built using Firestore and Firebase Functions, a NoSQL database and serverless app infrastructure respectively. Firestore provides a pretty handy async API for putting stuff in a database. async function putStuff(theStuff: any) {
// Optionally do data validation here
await firestore.collection(...).doc(...).set( theStuff );
} I then wrap this stuff in a Cloud Function. export my_function = functions.https.onCall(async (data, context) => {
await putStuff(data);
return 'Stuff has been put.';
}) The Firestore function call may throw an error in some circumstances, which then causes My issue is not pertaining to Cloud Functions itself, as the better solution is to use a try/catch block on my async function and handling that in a user-friendly manner. export my_function = functions.https.onCall(async (data, context) => {
try {
await putStuff(data);
} catch (e) {
return `Sorry, we cannot put the stuff. Error ${e}`;
}
return 'Stuff has been put.';
}) I do think ensuring that a function has a try/catch block is something that should be enforced at the compiler, under a particular optional flag, so that it signals to particular developers that there is code that should be handled before your execution returns to the framework. Particularly in a Node ecosystem moreso than other languages, the employment of many frameworks means that a higher-level solution would not be as scalable. |
@throws looks like a Decorator |
Are you aware of TSDoc and its definition of the TSDoc @throws tag? |
Suggestion
Use
@throws
to mark the corresponding error on the method in the .d.ts file in the lib directoryπ Search Terms
@throws
jsdoc
comments
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Use
@throws
to mark the corresponding error on the method in the .d.ts file in the lib directoryπ Motivating Example
/** * Converts a JavaScript Object Notation (JSON) string into an object. * @param text A valid JSON string. * @param reviver A function that transforms the results. This function is called for each member of the object. +* @throws {SyntaxError} if the string to parse is not valid JSON. * If a member contains nested objects, the nested objects are transformed before the parent object is. */ parse(text: string, reviver?: (this: any, key: string, value: any) => any): any;
π» Use Cases
want to be able to write code when you know that you need to beware of these unpredictable behavior
The text was updated successfully, but these errors were encountered: