Skip to content

Commit c72ad38

Browse files
authored
Merge pull request #12312 from CesiumGS/ts-files
DX: Fix `defined` and `Check` types for CesiumJS devs
2 parents 7d33049 + e1c2b91 commit c72ad38

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

gulpfile.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,12 +1198,19 @@ function generateTypeScriptDefinitions(
11981198
.replace(/^(\s*)(export )?const enum (\S+) {(\s*)$/gm, "$1$2enum $3 {$4")
11991199
// Replace JSDoc generation version of defined with an improved version using TS type predicates
12001200
.replace(
1201-
/defined\(value: any\): boolean/gm,
1202-
"defined<Type>(value: Type): value is NonNullable<Type>",
1201+
/\n?export function defined\(value: any\): boolean;/gm,
1202+
`\n${readFileSync("./packages/engine/Source/Core/defined.d.ts")
1203+
.toString()
1204+
.replace(/\n*\/\*.*?\*\/\n*/gms, "")
1205+
.replace("export default", "export")}`,
12031206
)
1207+
// Replace JSDoc generation version of Check with one that asserts the type of variables after called
12041208
.replace(
12051209
/\/\*\*[\*\s\w]*?\*\/\nexport const Check: any;/m,
1206-
`\n${readFileSync("./packages/engine/Source/Core/Check.d.ts").toString()}`,
1210+
`\n${readFileSync("./packages/engine/Source/Core/Check.d.ts")
1211+
.toString()
1212+
.replace(/export default.*\n?/, "")
1213+
.replace("const Check", "export const Check")}`,
12071214
)
12081215
// Fix https://github.com/CesiumGS/cesium/issues/10498 so we can use the rest parameter expand tuple
12091216
.replace(
@@ -1405,12 +1412,19 @@ function createTypeScriptDefinitions() {
14051412
.replace(/^(\s*)(export )?const enum (\S+) {(\s*)$/gm, "$1$2enum $3 {$4")
14061413
// Replace JSDoc generation version of defined with an improved version using TS type predicates
14071414
.replace(
1408-
/defined\(value: any\): boolean/gm,
1409-
"defined<Type>(value: Type): value is NonNullable<Type>",
1415+
/\n?export function defined\(value: any\): boolean;/gm,
1416+
`\n${readFileSync("./packages/engine/Source/Core/defined.d.ts")
1417+
.toString()
1418+
.replace(/\n*\/\*.*?\*\/\n*/gms, "")
1419+
.replace("export default", "export")}`,
14101420
)
1421+
// Replace JSDoc generation version of Check with one that asserts the type of variables after called
14111422
.replace(
14121423
/\/\*\*[\*\s\w]*?\*\/\nexport const Check: any;/m,
1413-
`\n${readFileSync("./packages/engine/Source/Core/Check.d.ts").toString()}`,
1424+
`\n${readFileSync("./packages/engine/Source/Core/Check.d.ts")
1425+
.toString()
1426+
.replace(/export default.*\n?/, "")
1427+
.replace("const Check", "export const Check")}`,
14141428
)
14151429
// Fix https://github.com/CesiumGS/cesium/issues/10498 to have rest parameter expand tuple
14161430
.replace(

packages/engine/Source/Core/Check.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Contains functions for checking that supplied arguments are of a specified type
33
* or meet specified conditions
44
*/
5-
export const Check: {
5+
const Check: {
66
/**
77
* Throws if test is not defined
88
*
@@ -125,3 +125,4 @@ export const Check: {
125125
};
126126
};
127127
};
128+
export default Check;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @example
3+
* if (Cesium.defined(positions)) {
4+
* doSomething();
5+
* } else {
6+
* doSomethingElse();
7+
* }
8+
* @param value - The object.
9+
* @returns Returns true if the object is defined, returns false otherwise.
10+
*/
11+
export default function defined<Type>(value: Type): value is NonNullable<Type>;

0 commit comments

Comments
 (0)