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

DX: Fix defined and Check types for CesiumJS devs #12312

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

jjspace
Copy link
Contributor

@jjspace jjspace commented Nov 15, 2024

Description

  • Add defined.d.ts which will now get picked up and provide the correct type predicates and assertions so JS Type checking will understand things are defined after this call
Before After
2024-11-15_16-11 2024-11-15_16-11_2
Before After
2024-11-15_16-14 2024-11-15_16-19
  • Updated the build process to account for these files and changes. The built Cesium.d.ts and engine/index.d.ts files should be identical to the previous ones.

Issue number and link

No issue, just a small DX issue I've grown too tired of seeing

Testing plan

  • In main run npm run built-ts
  • Create a copy of Source/Cesium.d.ts and packages/engine/index.d.ts somewhere you can check later
  • Switch to this branch and run npm run build-ts
  • Do a diff of those files and ensure they are identical. This confirms our external use will be the same downstream
  • To test locally find a usage of defined and Check locally
  • Turn on the VSCode setting Check JS for type checking with the TS server (This may or may not be necessary, I'm not sure)
  • Verify that in main the types aren't correct as shown above, verify that in this branch they are
  • Turn off the Check JS setting if you're not ready to see a lot of other (false positive) errors scattered in our code

Author checklist

  • I have submitted a Contributor License Agreement
  • I have added my name to CONTRIBUTORS.md
  • I have updated CHANGES.md with a short summary of my change
  • I have added or updated unit tests to ensure consistent code coverage
  • I have updated the inline documentation, and included code examples where relevant
  • I have performed a self-review of my code

Copy link

Thank you for the pull request, @jjspace!

✅ We can confirm we have a CLA on file for you.

Comment on lines +1415 to +1419
/\n?export function defined\(value: any\): boolean;/gm,
`\n${readFileSync("./packages/engine/Source/Core/defined.d.ts")
.toString()
.replace(/\n*\/\*.*?\*\/\n*/gms, "")
.replace("export default", "export")}`,
Copy link
Contributor Author

@jjspace jjspace Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per my comment when I did the same conversion into a .d.ts file for Check, I would love if we could skip this replace and pass the types file into tsd-jsdoc but we cannot. I think defined and Check are some of the only types we really need to override to take advantage of stuff like type predicates but if we add more we should definitely revisit this setup as it will quickly become too unwieldy.

@jjspace
Copy link
Contributor Author

jjspace commented Nov 19, 2024

@javagl Would you mind taking a look at this? I think you've been looking into our type system and typescript adjacent stuff recently so you might be a good person to provide input.

@jjspace jjspace requested a review from javagl November 19, 2024 19:14
Copy link
Contributor

@javagl javagl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the points from the testing plan. There are some smaller caveats.

After enabling the "Check JS" functionality in VS Code, it did complain about many things. But it did not generate the "...is possibly undefined" messages for me.
Maybe my VS Code is somehow messed up...? Maybe I'd have to delete something or use "a fresh copy" of something...? More generally: Where exactly is VSCode supposed to pull that new information from, when the d.ts files are supposed to be unchanged?

It did complain about the typeof in Check. And this is resolved by this change.
There may be an issue with the resulting export default in this case that likely will cause trouble in a TypeScript environment (see inlined comment). But this might be an easy fix.


Beyond that:

This is NOT in the scope of this PR, but I cannot look at the gulpfile.js without pointing out that this is one of my pet peeves 😬 The fact that it does have to contain these readFileSync(....) and ridiculously obscure regexes is bad enough. The fact that it has to contain them twice makes it even worse. But when looking at that duplication like in a blink comparator, and then noticing the difference in that // inlined comment, it is nearly funny again 🙂 :

Cesium Gulpfile duplication

Usually, I'm not touching that file, out of the fear of "breaking something". But ... after this is merged, I'll probably go out on a limb, take a pass at that file, and then there might be a function that is called fixUpTheOutputToMatchWhatWeNeed() - I'm reasonably sure that this would not break anything, at least...

gulpfile.js Outdated Show resolved Hide resolved
@jjspace
Copy link
Contributor Author

jjspace commented Nov 22, 2024

After enabling the "Check JS" functionality in VS Code, it did complain about many things. But it did not generate the "...is possibly undefined" messages for me.

@javagl I sometimes find I have to run the Restart TS Server or Reload Window commands to refresh typescript. They seem to cache some things internally or something that makes it get out of sync, especially in JS environments that aren't actually TS
2024-11-22_11-59_1
2024-11-22_11-59

This is NOT in the scope of this PR, but I cannot look at the gulpfile.js without pointing out that this is one of my pet peeves

I fully agree with this sentiment, I'd love if the logic was centralized because I always forget to change both any time I touch this. Definitely outside the scope of this PR though. I'm happy to review a PR for cleaning up the gulpfile but as you say, every time I touch this file I'm worried about breaking something

@javagl
Copy link
Contributor

javagl commented Nov 22, 2024

I sometimes find I have to run the Restart TS Server or Reload Window commands to refresh typescript.

Yeah, I also had cases where this was necessary. But it wasnt't the root cause here. Although I did search for "defined(" and did click through many cases, I didn't click to all (13509) appearances. And apparently, I only looked at ones that didn't show the message because there was no corresponding information in the JSDoc: Only when that "thing" is defined to be a non-undefined type via JSDoc, the message appears. For example, in this snippet...

/**
 * Does not complain about s potentially being undefined
 */
function exampleA(s) {
  if (defined(s)) {
    console.log(s.length);
  }
}
/**
 * Does not complain about s potentially being undefined
 * 
 * @param {string} s
 */
function exampleB(s) {
  if (defined(s)) {
    console.log(s.length);
  }
}

/**
 * DOES complain about s potentially being undefined!
 * 
 * @param {string | undefined} s
 */
function exampleC(s) {
  if (defined(s)) {
    console.log(s.length);
  }
}

it shows the message for the bottommost one:

Cesium Defined Check 2

And I confirmed that this does no longer appear on the branch of this PR.

@jjspace
Copy link
Contributor Author

jjspace commented Nov 25, 2024

@javagl glad you were able to confirm the issue and this fixes it for you too. Do you have any other comments on this?

Copy link
Contributor

@javagl javagl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jjspace I assume that this was about the pending review approval...?
Tested it and it works as intended. I think that it can be merged.

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.

2 participants