Skip to content

Add TypeScript typings #8

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

Merged
merged 1 commit into from
Sep 30, 2019
Merged

Conversation

rplopes
Copy link
Contributor

@rplopes rplopes commented Jul 5, 2019

Adding types to common dependencies we maintain should improve developer experience on projects that use them (at least when using tools such as Code's IntelliSense). It would also make it easier to switch some of those projects to TypeScript if we decided to migrate them.

Generated by converting the project's files to TypeScript and running the compiler and also by taking clues from @types/standard-http-error.

@rplopes rplopes requested review from kurayama and Americas July 5, 2019 10:44
@rplopes rplopes self-assigned this Jul 5, 2019
@waldyrious
Copy link

Output of tsc --allowJs --checkJs --noEmit src/**/*.js:

src/errors/bad-request-error.js:19:11 - error TS2556: Expected 0 arguments, but got 2 or more.
19     super(400, ...arguments);
             ~~~~~~~~~~~~~~~~~

src/errors/bad-request-error.js:19:19 - error TS2461: Type 'IArguments' is not an array type.
19     super(400, ...arguments);
                     ~~~~~~~~~

src/errors/conflict-error.js:19:11 - error TS2556: Expected 0 arguments, but got 2 or more.
19     super(409, ...arguments);
             ~~~~~~~~~~~~~~~~~

src/errors/conflict-error.js:19:19 - error TS2461: Type 'IArguments' is not an array type.
19     super(409, ...arguments);
                     ~~~~~~~~~

src/errors/forbidden-error.js:19:11 - error TS2556: Expected 0 arguments, but got 2 or more.
19     super(403, ...arguments);
             ~~~~~~~~~~~~~~~~~

src/errors/forbidden-error.js:19:19 - error TS2461: Type 'IArguments' is not an array type.
19     super(403, ...arguments);
                     ~~~~~~~~~

src/errors/gone-error.js:19:11 - error TS2556: Expected 0 arguments, but got 2 or more.
19     super(410, ...arguments);
             ~~~~~~~~~~~~~~~~~

src/errors/gone-error.js:19:19 - error TS2461: Type 'IArguments' is not an array type.
19     super(410, ...arguments);
                     ~~~~~~~~~

src/errors/not-found-error.js:19:11 - error TS2556: Expected 0 arguments, but got 2 or more.
19     super(404, ...arguments);
             ~~~~~~~~~~~~~~~~~

src/errors/not-found-error.js:19:19 - error TS2461: Type 'IArguments' is not an array type.
19     super(404, ...arguments);
                     ~~~~~~~~~

src/errors/service-unavailable-error.js:19:11 - error TS2556: Expected 0 arguments, but got 2 or more.
19     super(503, ...arguments);
             ~~~~~~~~~~~~~~~~~

src/errors/service-unavailable-error.js:19:19 - error TS2461: Type 'IArguments' is not an array type.
19     super(503, ...arguments);
                     ~~~~~~~~~

src/errors/unauthorized-error.js:19:11 - error TS2556: Expected 0 arguments, but got 2 or more.
19     super(401, ...arguments);
             ~~~~~~~~~~~~~~~~~

src/errors/unauthorized-error.js:19:19 - error TS2461: Type 'IArguments' is not an array type.
19     super(401, ...arguments);
                     ~~~~~~~~~

src/errors/validation-failed-error.js:19:11 - error TS2554: Expected 0 arguments, but got 3.
19     super(400, 'Validation Failed', { errors });
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 15 errors.

Btw, maybe we should include that command in the travis script?

@rplopes
Copy link
Contributor Author

rplopes commented Jul 5, 2019

@waldyrious trying to run the TypeScript compiler on JavaScript code is prone to failures like those because of all the expectations that the JavaScript code can't deliver on without the added information in TypeScript's syntax.

Example:

src/errors/bad-request-error.js:19:11 - error TS2556: Expected 0 arguments, but got 2 or more.
19     super(400, ...arguments);

Here, we're passing (at least) 2 arguments to HttpError.constructor(). Because that method is not explicitely set, the TypeScript compiler has no information about the expected arguments it receives. With TypeScript code we could have typings informing the compiler, but with JavaScript we don't. Therefore, the compiler is assuming that constructor is empty (i.e. accepts 0 arguments), and is throwing an error because it sees a usage with 2 arguments, which is a violation to that rule.

So, long story short, it's expected to see many of those errors if we're using the TypeScript compiler on JavaScript files, and it's actually one of the motivators for coming up with types in these common dependencies: so that when we run the compiler on other projects, we don't get that sort of errors.

@waldyrious
Copy link

the TypeScript compiler has no information about the expected arguments it receives. With TypeScript code we could have typings informing the compiler, but with JavaScript we don't.

Hmm, that shouldn't be the case -- providing that information is precisely the role of the .d.ts files. In particular, HttpError is a mere alias to StandardHttpError, whose constructor has type definitions here.

It looks like the main issue here is that it's not trivial to make tsc behave like whatever VSCode has set up for its intellisense. Let's hope the situation will improve in the future. In any case, these changes are already a step forward. 👍

@Americas Americas merged commit 917a198 into master Sep 30, 2019
@Americas Americas deleted the support/add-typescript-typings branch September 30, 2019 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants