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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1198,12 +1198,19 @@ function generateTypeScriptDefinitions(
.replace(/^(\s*)(export )?const enum (\S+) {(\s*)$/gm, "$1$2enum $3 {$4")
// Replace JSDoc generation version of defined with an improved version using TS type predicates
.replace(
/defined\(value: any\): boolean/gm,
"defined<Type>(value: Type): value is NonNullable<Type>",
/\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")}`,
)
// Replace JSDoc generation version of Check with one that asserts the type of variables after called
.replace(
/\/\*\*[\*\s\w]*?\*\/\nexport const Check: any;/m,
`\n${readFileSync("./packages/engine/Source/Core/Check.d.ts").toString()}`,
`\n${readFileSync("./packages/engine/Source/Core/Check.d.ts")
.toString()
.replace(/export default.*\n?/, "")
.replace("const Check", "export const Check")}`,
)
// Fix https://github.com/CesiumGS/cesium/issues/10498 so we can use the rest parameter expand tuple
.replace(
Expand Down Expand Up @@ -1405,12 +1412,19 @@ function createTypeScriptDefinitions() {
.replace(/^(\s*)(export )?const enum (\S+) {(\s*)$/gm, "$1$2enum $3 {$4")
// Replace JSDoc generation version of defined with an improved version using TS type predicates
.replace(
/defined\(value: any\): boolean/gm,
"defined<Type>(value: Type): value is NonNullable<Type>",
/\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")}`,
Comment on lines +1415 to +1419
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.

)
// Replace JSDoc generation version of Check with one that asserts the type of variables after called
.replace(
/\/\*\*[\*\s\w]*?\*\/\nexport const Check: any;/m,
`\n${readFileSync("./packages/engine/Source/Core/Check.d.ts").toString()}`,
`\n${readFileSync("./packages/engine/Source/Core/Check.d.ts")
.toString()
.replace(/export default.*\n?/, "")
.replace("const Check", "export const Check")}`,
)
// Fix https://github.com/CesiumGS/cesium/issues/10498 to have rest parameter expand tuple
.replace(
Expand Down
3 changes: 2 additions & 1 deletion packages/engine/Source/Core/Check.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Contains functions for checking that supplied arguments are of a specified type
* or meet specified conditions
*/
export const Check: {
const Check: {
/**
* Throws if test is not defined
*
Expand Down Expand Up @@ -125,3 +125,4 @@ export const Check: {
};
};
};
export default Check;
11 changes: 11 additions & 0 deletions packages/engine/Source/Core/defined.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @example
* if (Cesium.defined(positions)) {
* doSomething();
* } else {
* doSomethingElse();
* }
* @param value - The object.
* @returns Returns true if the object is defined, returns false otherwise.
*/
export default function defined<Type>(value: Type): value is NonNullable<Type>;