totaljs / framework4

Total.js framework v4
https://www.totaljs.com
Other
99 stars 36 forks source link

Imporved Typescript types #51

Closed willful759 closed 1 year ago

willful759 commented 1 year ago

I finished the frontend of our current site, but now that i'm trying begin creating the backend using typescript, some sore spots of the current defined types have emerged, so I wrote some fixes

Quick Rundown

The most important change

currently, globals are exported this way:

declare function ROUTE(...);

This actually doesn't work, while you can import them "fine", and even typechecks, using something like:

import { ROUTE } from "total4";

export function install() {
    ROUTE("GET /");
}

Typescript actually compiles the following code:

const total4_1 = require("total4");
function install() {
    (0, total4_1.ROUTE)("GET /");
}
exports.install = install;

Which causes an exception once you try to actually run the program:

TypeError: (0 , total4_1.ROUTE) is not a function

Simply changing the declarations to:

declare global {
    ....
    function ROUTE(...);
    ...
}

Fixed this issue, and now it compiles and typechecks properly.

Other changes

This is not at all an exhaustive list of all the problems there might be, this are just the problems I ran into while trying to get this running, i'm also not a typescript pro at all, as a matter of fact this will be my first project, so I might have just been doing things wrong, I did these changes because no matter how I try, I can't get this to work without them. at least not without basically creating my on .d.ts file. I understand typescript is not really a supported use cause, just doing what I can to improve the library.

If no other change makes it, I would really appreciate if the change to the way globals are exported is made is kept.

petersirka commented 1 year ago

Great job!