Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue
Milestone
Description
TypeScript Version: 2.4.2
Code
This code does not work when using noImplicitAny :
const buffer = crypto.getRandomValues(new Uint8Array(31))
// Error : Element implicitly has an 'any' type because type 'ArrayBufferView' has no index signature.
const value = buffer[0]To make it work we need to make an explicit cast:
const buffer = crypto.getRandomValues(new Uint8Array(31)) as Uint8Array
// Works
const value = buffer[0]This is because crypto.getRandomValues is defined like this in lib.dom.d.ts :
interface RandomSource {
getRandomValues(array: ArrayBufferView): ArrayBufferView;
}We can prevent this problem by using generics as the array parameter is returned:
interface RandomSource {
getRandomValues<T extends ArrayBufferView>(array: T): T;
}[off-topic] BTW isn't the index signature of an ArrayBufferView always a number no matter the internal type? [/off-topic]
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue
Activity
weswigham commentedon Aug 5, 2017
Tracked here.
sandersn commentedon Aug 9, 2017
This is fixed now. When do we close issues like this? When we update the LKG? There is only one other open, and it's from Jun 15.
weswigham commentedon Aug 9, 2017
Nathan Shively-Sanders (@sandersn) I think so, but I'm not sure? Mohamed Hegazy (@mhegazy) would know what the convention is, I assume.
fathyb commentedon Aug 9, 2017
Just found out
crypto.getRandomValuesdoes not take allArrayBufferViews, only the integer ones according to the last recommendation.To be safer the correct code should be :
weswigham commentedon Aug 9, 2017
Fathy Boundjadj (@fathyb) tracked.