-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Interface Crypto does not implement RandomSource, so Crypto.getRandomValues is not generic #23646
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
Interface Crypto does not implement RandomSource, so Crypto.getRandomValues is not generic #23646
Comments
@viceice Based on spec - Crypto#getRandomValues, I think, we do not need to extend Crypto interface from the RandomSource interface. ArrayBufferView getRandomValues(ArrayBufferView array);
Spec says that type ArrayBufferView = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | DataView; |
At typescript 2.7 interface Crypto extends Object, RandomSource {
readonly subtle: SubtleCrypto;
}
interface RandomSource {
getRandomValues<T extends Int8Array | Uint8ClampedArray | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array>(array: T): T;
} With typescript 2.8 it has changed to interface Crypto {
readonly subtle: SubtleCrypto;
getRandomValues(array: Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array | DataView | null): Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array | DataView | null;
} This looks wrong to me. |
Ok, aggreed. At https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues step 4. it says, that it returns the given array. So maybe we can make this method generic? Like before, but with the new types. So that the return value will be like the given type and not a multi type? interface Crypto {
readonly subtle: SubtleCrypto;
type ArrayBufferView = Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array | DataView | null;
getRandomValues<T extends ArrayBufferView = ArrayBufferView>(array: T): T;
} |
PRs welcomed. You can find more information about contributing lib.d.ts fixes at https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes. |
I still cannot reference |
TypeScript Version: 2.8.3
Search Terms:
Code
Expected behavior:
Get a concated string of 2 random uint32.
Actual behavior:
Compiler error:
Property 'join' does not exist on type 'Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray ...'. Property 'join' does not exist on type 'DataView'.
Looks like microsoft/TypeScript-DOM-lib-generator#278 is not included or overwritten by someone.
I suggest to simply extend the
Crypto
interface from theRandomSource
interface.Playground Link:
https://www.typescriptlang.org/play/#src=crypto.getRandomValues(new%20Uint32Array(2)).join('')
Related Issues:
#17610
The text was updated successfully, but these errors were encountered: