Skip to content
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

test: New "typecheck" utility #21248

Closed
wants to merge 1 commit into from

Conversation

mvollmer
Copy link
Member

@mvollmer mvollmer commented Nov 11, 2024

That's my current idea for how I think I would like to start moving code to TypeScript. Let's talk about it.

  • call it from test/static-code

@mvollmer
Copy link
Member Author

mvollmer commented Nov 11, 2024

I wrote down the following notes, but I am pretty unsure still about the whole approach... maybe just move one file at a time directly to full-strict TypeScript is just fine?

  • Any help with finding bugs is welcome, and TypeScript is a great
    help. Kinda like Coverity for JavaScript. The type inference
    algorithms is very capable.

  • Non-strict mode seems to be too weak. It doesn't seem to find
    much. Doesn't find things that might be unexpectedly null, for
    example. So we need to be strict.

  • The inference algorithm seems to work differently for different
    strict-ness levels. Turning noImplicitAny off will infer "never[]"
    instead of "any[]" for "const x = []", for example. So its probably
    best not to open this particular can of worms and always use the
    same options.

  • We can only move our code base over to TypeScript one file at a
    time. Doing it all at once seems too much.

  • So we only make a couple files "strict clean" and grow that set as
    fast as we feel like. This means renaming them to *.ts or *.tsx
    since only those files can get type annotated, and we need type
    annotations to make them "strict clean". (And putting things
    temporarily into *.d.ts files seems like a distraction.)

  • But it would be great if the rest of the files (the not-yet
    strict-clean ones) could also contribute to the bug hunt already
    now, as they are. The types in the strict-clean files could be
    used to check whether the non-clean files use their API correctly,
    and can also in turn be used to check that the type annotations in
    the strict-clean files are actually correct.

  • This seems hopeless, however. The non-clean files produce an
    enormous amount of errors (when treated as strict TypeScript).

  • But there seems to be a useful middle ground: strict-mode but
    without noImplicitAny. Making a file typecheck clean in strict
    mode without noImplicitAny seems to be reasonable, and even fun.
    Instead of 150 errors that make you dig into the code and come up
    with the best types you can, you only get 20 pretty easy ones.

  • We don't want to switch off noImplicitAny globally, however. We
    really want to keep the strict-clean files clean. (And see above
    how the inference algorithm would change.)

  • But we (I, at least) also want to grow the set of files that
    participate in the typechecking bug hunt faster than we can grow
    the set of strict-clean files.

  • So let's invent the comment annotation that TypeScript itself
    refuses to implement: per-file strictness rules.

  • But only one, for now: @cockpit-ts-relaxed. If you put this in a
    file at the top, the most annoying errors relating to implicit
    'any' types will be ignored, for that file only.

  • This seems to be in the spirit of how TypeScript thinks about
    gradual typing: Check all the files, including the JS ones, and
    just ignore the errors you don't care about. This is viable (in
    TS's opinion) because even with errors you'll get your generated
    JS output.

@mvollmer
Copy link
Member Author

part of #21265 now

@mvollmer mvollmer closed this Nov 15, 2024
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.

1 participant