Skip to content

Commit

Permalink
create defined types, update Check types, update build
Browse files Browse the repository at this point in the history
  • Loading branch information
jjspace committed Nov 15, 2024
1 parent 91821cc commit 587f65a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
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")}`,
)
// 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>;

0 comments on commit 587f65a

Please sign in to comment.