-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[js/common] use TS type inference to eliminate
unknown
(#23012)
### Description This change uses a TypeScript trick to infer global types in onnxruntime-common. Thanks to the strong type system of TypeScript, we are able to refer to types that may not be available in the context. This helps to keep onnxruntime-common not to include dependencies like "@webgpu/types", and still being able to use the types in the declaration. See comments of `TryGetGlobalType` in `type-helper.ts`.
- Loading branch information
Showing
6 changed files
with
45 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
/** | ||
* A helper type to get certain types if they are declared in global scope. | ||
* | ||
* For example, if you installed "@webgpu/types" as a dev dependency, then `TryGetTypeIfDeclared<'GPUDevice'>` will | ||
* be type `GPUDevice`, otherwise it will be type `unknown`. | ||
* | ||
* | ||
* We don't want to introduce "@webgpu/types" as a dependency of this package because: | ||
* | ||
* (1) For JavaScript users, it's not needed. For TypeScript users, they can install it as dev dependency themselves. | ||
* | ||
* (2) because "@webgpu/types" requires "@types/dom-webcodecs" as peer dependency when using TypeScript < v5.1 and its | ||
* version need to be chosen carefully according to the TypeScript version being used. This means so far there is not a | ||
* way to keep every TypeScript version happy. It turns out that we will easily broke users on some TypeScript version. | ||
* | ||
* for more info see https://github.com/gpuweb/types/issues/127 | ||
* | ||
* Update (2024-08-07): The reason (2) may be no longer valid. Most people should be using TypeScript >= 5.1 by now. | ||
* However, we are still not sure whether introducing "@webgpu/types" as direct dependency is a good idea. We find this | ||
* type helper is useful for TypeScript users. | ||
* | ||
* @ignore | ||
*/ | ||
export type TryGetGlobalType<Name extends string, Fallback = unknown> = typeof globalThis extends { | ||
[k in Name]: { prototype: infer T }; | ||
} | ||
? T | ||
: Fallback; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters