Skip to content

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

Closed
viceice opened this issue Apr 24, 2018 · 6 comments · Fixed by microsoft/TypeScript-DOM-lib-generator#453
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this

Comments

@viceice
Copy link

viceice commented Apr 24, 2018

TypeScript Version: 2.8.3

Search Terms:

  • getRandomValues

Code

crypto.getRandomValues(new Uint32Array(2)).join('')

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 the RandomSource interface.

Playground Link:

https://www.typescriptlang.org/play/#src=crypto.getRandomValues(new%20Uint32Array(2)).join('')

Related Issues:
#17610

@a-tarasyuk
Copy link
Contributor

a-tarasyuk commented Apr 24, 2018

@viceice Based on spec - Crypto#getRandomValues, I think, we do not need to extend Crypto interface from the RandomSource interface.

ArrayBufferView getRandomValues(ArrayBufferView array);

getRandomValues returns ArrayBufferView

ArrayBufferView is a helper type representing any of the following JavaScript TypedArray types: Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array or DataView. This is a helper type to simplify the specification, it isn't an interface and there are no objects implementing it.
typedef ArrayBufferView

Spec says that ArrayBufferView - is a helper type, and I suppose that it should look like so

type ArrayBufferView = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | DataView;

@viceice
Copy link
Author

viceice commented Apr 24, 2018

At typescript 2.7 Crypto interface was defined at lib.d.ts:

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.

@a-tarasyuk
Copy link
Contributor

@viceice Seems that the latest changes follow the spec

[Exposed=(Window,Worker)]
interface Crypto {
  [SecureContext] readonly attribute SubtleCrypto subtle;
  ArrayBufferView getRandomValues(ArrayBufferView array);
};

Crypto does not extend RandomSource.

@viceice
Copy link
Author

viceice commented Apr 24, 2018

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;
}

@mhegazy mhegazy added Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this labels Apr 24, 2018
@mhegazy mhegazy added this to the Community milestone Apr 24, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Apr 24, 2018

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.

@mhegazy mhegazy added the Fixed in TSJS repo Fix merged in https://github.com/Microsoft/TSJS-lib-generator, but not ported yet label Apr 25, 2018
@mhegazy mhegazy self-assigned this Apr 25, 2018
@mhegazy mhegazy modified the milestones: Community, TypeScript 2.9 Apr 25, 2018
@mhegazy mhegazy reopened this Apr 25, 2018
@mhegazy mhegazy modified the milestones: TypeScript 2.9, TypeScript 3.0 May 8, 2018
@mhegazy mhegazy added Fixed A PR has been merged for this issue and removed Fixed in TSJS repo Fix merged in https://github.com/Microsoft/TSJS-lib-generator, but not ported yet labels Jun 5, 2018
@mrowles
Copy link

mrowles commented Mar 29, 2019

I still cannot reference crypto.getRandomValues or straight getRandomValues, throws unknown library in NodeJS after compilation. Has anyone figured this out nicely?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants