diff --git a/tsc/internal/bundled/libs/lib.dom.d.ts b/tsc/internal/bundled/libs/lib.dom.d.ts index 9e127c3f5a524..55b1eb056bf74 100644 --- a/tsc/internal/bundled/libs/lib.dom.d.ts +++ b/tsc/internal/bundled/libs/lib.dom.d.ts @@ -11170,7 +11170,7 @@ interface Crypto { * * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Crypto/getRandomValues) */ - getRandomValues>(array: T): T; + getRandomValues(array: T): T; /** * The **`randomUUID()`** method of the Crypto interface is used to generate a v4 UUID using a cryptographically secure random number generator. * Available only in secure contexts. diff --git a/tsc/internal/bundled/libs/lib.es2020.bigint.d.ts b/tsc/internal/bundled/libs/lib.es2020.bigint.d.ts index fde848f721684..8000584f799e0 100644 --- a/tsc/internal/bundled/libs/lib.es2020.bigint.d.ts +++ b/tsc/internal/bundled/libs/lib.es2020.bigint.d.ts @@ -722,6 +722,11 @@ interface BigUint64ArrayConstructor { } declare var BigUint64Array: BigUint64ArrayConstructor; +interface IntegerTypedArrayTypes { + BigInt64Array: BigInt64Array; + BigUint64Array: BigUint64Array; +} + interface DataView { /** * Gets the BigInt64 value at the specified byte offset from the start of the view. There is diff --git a/tsc/internal/bundled/libs/lib.es5.d.ts b/tsc/internal/bundled/libs/lib.es5.d.ts index e394111cb0e6c..5544db0e0b4d2 100644 --- a/tsc/internal/bundled/libs/lib.es5.d.ts +++ b/tsc/internal/bundled/libs/lib.es5.d.ts @@ -4409,6 +4409,21 @@ interface Float64ArrayConstructor { } declare var Float64Array: Float64ArrayConstructor; +/** + * Stores the typed array types that hold integers, such as those accepted by `crypto.getRandomValues`. + */ +interface IntegerTypedArrayTypes { + Int8Array: Int8Array; + Int16Array: Int16Array; + Int32Array: Int32Array; + Uint8Array: Uint8Array; + Uint16Array: Uint16Array; + Uint32Array: Uint32Array; + Uint8ClampedArray: Uint8ClampedArray; +} + +type IntegerTypedArray = IntegerTypedArrayTypes[keyof IntegerTypedArrayTypes]; + ///////////////////////////// /// ECMAScript Internationalization API ///////////////////////////// diff --git a/tsc/internal/bundled/libs/lib.webworker.d.ts b/tsc/internal/bundled/libs/lib.webworker.d.ts index 45c6a6134821d..1534323fa4784 100644 --- a/tsc/internal/bundled/libs/lib.webworker.d.ts +++ b/tsc/internal/bundled/libs/lib.webworker.d.ts @@ -3183,7 +3183,7 @@ interface Crypto { * * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Crypto/getRandomValues) */ - getRandomValues>(array: T): T; + getRandomValues(array: T): T; /** * The **`randomUUID()`** method of the Crypto interface is used to generate a v4 UUID using a cryptographically secure random number generator. * Available only in secure contexts. diff --git a/tsc/internal/fourslash/tests/util/util.go b/tsc/internal/fourslash/tests/util/util.go index 6c2285301d78c..b44b9174b0716 100644 --- a/tsc/internal/fourslash/tests/util/util.go +++ b/tsc/internal/fourslash/tests/util/util.go @@ -1162,6 +1162,16 @@ var CompletionGlobalTypeDecls = []fourslash.CompletionsExpectedItem{ Kind: new(lsproto.CompletionItemKindInterface), SortText: new(string(ls.SortTextGlobalsOrKeywords)), }, + &lsproto.CompletionItem{ + Label: "IntegerTypedArray", + Kind: new(lsproto.CompletionItemKindClass), + SortText: new(string(ls.SortTextGlobalsOrKeywords)), + }, + &lsproto.CompletionItem{ + Label: "IntegerTypedArrayTypes", + Kind: new(lsproto.CompletionItemKindInterface), + SortText: new(string(ls.SortTextGlobalsOrKeywords)), + }, &lsproto.CompletionItem{ Label: "Intl", Kind: new(lsproto.CompletionItemKindModule), diff --git a/tsc/testdata/baselines/reference/compiler/integerTypedArray.errors.txt b/tsc/testdata/baselines/reference/compiler/integerTypedArray.errors.txt new file mode 100644 index 0000000000000..3604a28fcc75e --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/integerTypedArray.errors.txt @@ -0,0 +1,30 @@ +integerTypedArray.ts(3,24): error TS2345: Argument of type 'Float64Array' is not assignable to parameter of type 'IntegerTypedArray'. + Type 'Float64Array' is not assignable to type 'Uint8ClampedArray'. + The types returned by 'filter(...)[Symbol.toStringTag]' are incompatible between these types. + Type '"Float64Array"' is not assignable to type '"Uint8ClampedArray"'. +integerTypedArray.ts(4,24): error TS2345: Argument of type 'null' is not assignable to parameter of type 'IntegerTypedArray'. + + +==== integerTypedArray.ts (2 errors) ==== + crypto.getRandomValues(new Uint8Array(1)); + crypto.getRandomValues(new BigInt64Array(1)); + crypto.getRandomValues(new Float64Array(1)); + ~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type 'Float64Array' is not assignable to parameter of type 'IntegerTypedArray'. +!!! error TS2345: Type 'Float64Array' is not assignable to type 'Uint8ClampedArray'. +!!! error TS2345: The types returned by 'filter(...)[Symbol.toStringTag]' are incompatible between these types. +!!! error TS2345: Type '"Float64Array"' is not assignable to type '"Uint8ClampedArray"'. + crypto.getRandomValues(null); + ~~~~ +!!! error TS2345: Argument of type 'null' is not assignable to parameter of type 'IntegerTypedArray'. + + declare const buffer: Uint8Array; + crypto.getRandomValues(buffer); + + declare const array: Parameters[0]; + const values = crypto.getRandomValues(array); + values.BYTES_PER_ELEMENT; + + declare const integers: IntegerTypedArray; + const ints: IntegerTypedArrayTypes["Int32Array"] = new Int32Array(1); + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/integerTypedArray.js b/tsc/testdata/baselines/reference/compiler/integerTypedArray.js new file mode 100644 index 0000000000000..f183d258c3db4 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/integerTypedArray.js @@ -0,0 +1,29 @@ +//// [tests/cases/compiler/integerTypedArray.ts] //// + +//// [integerTypedArray.ts] +crypto.getRandomValues(new Uint8Array(1)); +crypto.getRandomValues(new BigInt64Array(1)); +crypto.getRandomValues(new Float64Array(1)); +crypto.getRandomValues(null); + +declare const buffer: Uint8Array; +crypto.getRandomValues(buffer); + +declare const array: Parameters[0]; +const values = crypto.getRandomValues(array); +values.BYTES_PER_ELEMENT; + +declare const integers: IntegerTypedArray; +const ints: IntegerTypedArrayTypes["Int32Array"] = new Int32Array(1); + + +//// [integerTypedArray.js] +"use strict"; +crypto.getRandomValues(new Uint8Array(1)); +crypto.getRandomValues(new BigInt64Array(1)); +crypto.getRandomValues(new Float64Array(1)); +crypto.getRandomValues(null); +crypto.getRandomValues(buffer); +const values = crypto.getRandomValues(array); +values.BYTES_PER_ELEMENT; +const ints = new Int32Array(1); diff --git a/tsc/testdata/baselines/reference/compiler/integerTypedArray.symbols b/tsc/testdata/baselines/reference/compiler/integerTypedArray.symbols new file mode 100644 index 0000000000000..adcb98dddd4b2 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/integerTypedArray.symbols @@ -0,0 +1,65 @@ +//// [tests/cases/compiler/integerTypedArray.ts] //// + +=== integerTypedArray.ts === +crypto.getRandomValues(new Uint8Array(1)); +>crypto.getRandomValues : Symbol(Crypto.getRandomValues, Decl(lib.dom.d.ts, --, --)) +>crypto : Symbol(crypto, Decl(lib.dom.d.ts, --, --)) +>getRandomValues : Symbol(Crypto.getRandomValues, Decl(lib.dom.d.ts, --, --)) +>Uint8Array : Symbol(Uint8Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --) ... and 4 more) + +crypto.getRandomValues(new BigInt64Array(1)); +>crypto.getRandomValues : Symbol(Crypto.getRandomValues, Decl(lib.dom.d.ts, --, --)) +>crypto : Symbol(crypto, Decl(lib.dom.d.ts, --, --)) +>getRandomValues : Symbol(Crypto.getRandomValues, Decl(lib.dom.d.ts, --, --)) +>BigInt64Array : Symbol(BigInt64Array, Decl(lib.es2020.bigint.d.ts, --, --), Decl(lib.es2020.bigint.d.ts, --, --), Decl(lib.es2022.array.d.ts, --, --), Decl(lib.es2023.array.d.ts, --, --)) + +crypto.getRandomValues(new Float64Array(1)); +>crypto.getRandomValues : Symbol(Crypto.getRandomValues, Decl(lib.dom.d.ts, --, --)) +>crypto : Symbol(crypto, Decl(lib.dom.d.ts, --, --)) +>getRandomValues : Symbol(Crypto.getRandomValues, Decl(lib.dom.d.ts, --, --)) +>Float64Array : Symbol(Float64Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --) ... and 3 more) + +crypto.getRandomValues(null); +>crypto.getRandomValues : Symbol(Crypto.getRandomValues, Decl(lib.dom.d.ts, --, --)) +>crypto : Symbol(crypto, Decl(lib.dom.d.ts, --, --)) +>getRandomValues : Symbol(Crypto.getRandomValues, Decl(lib.dom.d.ts, --, --)) + +declare const buffer: Uint8Array; +>buffer : Symbol(buffer, Decl(integerTypedArray.ts, 5, 13)) +>Uint8Array : Symbol(Uint8Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --) ... and 4 more) +>ArrayBufferLike : Symbol(ArrayBufferLike, Decl(lib.es5.d.ts, --, --)) + +crypto.getRandomValues(buffer); +>crypto.getRandomValues : Symbol(Crypto.getRandomValues, Decl(lib.dom.d.ts, --, --)) +>crypto : Symbol(crypto, Decl(lib.dom.d.ts, --, --)) +>getRandomValues : Symbol(Crypto.getRandomValues, Decl(lib.dom.d.ts, --, --)) +>buffer : Symbol(buffer, Decl(integerTypedArray.ts, 5, 13)) + +declare const array: Parameters[0]; +>array : Symbol(array, Decl(integerTypedArray.ts, 8, 13)) +>Parameters : Symbol(Parameters, Decl(lib.es5.d.ts, --, --)) +>crypto.getRandomValues : Symbol(Crypto.getRandomValues, Decl(lib.dom.d.ts, --, --)) +>crypto : Symbol(crypto, Decl(lib.dom.d.ts, --, --)) +>getRandomValues : Symbol(Crypto.getRandomValues, Decl(lib.dom.d.ts, --, --)) + +const values = crypto.getRandomValues(array); +>values : Symbol(values, Decl(integerTypedArray.ts, 9, 5)) +>crypto.getRandomValues : Symbol(Crypto.getRandomValues, Decl(lib.dom.d.ts, --, --)) +>crypto : Symbol(crypto, Decl(lib.dom.d.ts, --, --)) +>getRandomValues : Symbol(Crypto.getRandomValues, Decl(lib.dom.d.ts, --, --)) +>array : Symbol(array, Decl(integerTypedArray.ts, 8, 13)) + +values.BYTES_PER_ELEMENT; +>values.BYTES_PER_ELEMENT : Symbol(BYTES_PER_ELEMENT, Decl(lib.es2020.bigint.d.ts, --, --), Decl(lib.es2020.bigint.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --) ... and 4 more) +>values : Symbol(values, Decl(integerTypedArray.ts, 9, 5)) +>BYTES_PER_ELEMENT : Symbol(BYTES_PER_ELEMENT, Decl(lib.es2020.bigint.d.ts, --, --), Decl(lib.es2020.bigint.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --) ... and 4 more) + +declare const integers: IntegerTypedArray; +>integers : Symbol(integers, Decl(integerTypedArray.ts, 12, 13)) +>IntegerTypedArray : Symbol(IntegerTypedArray, Decl(lib.es5.d.ts, --, --)) + +const ints: IntegerTypedArrayTypes["Int32Array"] = new Int32Array(1); +>ints : Symbol(ints, Decl(integerTypedArray.ts, 13, 5)) +>IntegerTypedArrayTypes : Symbol(IntegerTypedArrayTypes, Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.bigint.d.ts, --, --)) +>Int32Array : Symbol(Int32Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --) ... and 3 more) + diff --git a/tsc/testdata/baselines/reference/compiler/integerTypedArray.types b/tsc/testdata/baselines/reference/compiler/integerTypedArray.types new file mode 100644 index 0000000000000..25a43575154c0 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/integerTypedArray.types @@ -0,0 +1,74 @@ +//// [tests/cases/compiler/integerTypedArray.ts] //// + +=== integerTypedArray.ts === +crypto.getRandomValues(new Uint8Array(1)); +>crypto.getRandomValues(new Uint8Array(1)) : Uint8Array +>crypto.getRandomValues : (array: T) => T +>crypto : Crypto +>getRandomValues : (array: T) => T +>new Uint8Array(1) : Uint8Array +>Uint8Array : Uint8ArrayConstructor +>1 : 1 + +crypto.getRandomValues(new BigInt64Array(1)); +>crypto.getRandomValues(new BigInt64Array(1)) : BigInt64Array +>crypto.getRandomValues : (array: T) => T +>crypto : Crypto +>getRandomValues : (array: T) => T +>new BigInt64Array(1) : BigInt64Array +>BigInt64Array : BigInt64ArrayConstructor +>1 : 1 + +crypto.getRandomValues(new Float64Array(1)); +>crypto.getRandomValues(new Float64Array(1)) : IntegerTypedArray +>crypto.getRandomValues : (array: T) => T +>crypto : Crypto +>getRandomValues : (array: T) => T +>new Float64Array(1) : Float64Array +>Float64Array : Float64ArrayConstructor +>1 : 1 + +crypto.getRandomValues(null); +>crypto.getRandomValues(null) : IntegerTypedArray +>crypto.getRandomValues : (array: T) => T +>crypto : Crypto +>getRandomValues : (array: T) => T + +declare const buffer: Uint8Array; +>buffer : Uint8Array + +crypto.getRandomValues(buffer); +>crypto.getRandomValues(buffer) : Uint8Array +>crypto.getRandomValues : (array: T) => T +>crypto : Crypto +>getRandomValues : (array: T) => T +>buffer : Uint8Array + +declare const array: Parameters[0]; +>array : IntegerTypedArray +>crypto.getRandomValues : (array: T) => T +>crypto : Crypto +>getRandomValues : (array: T) => T + +const values = crypto.getRandomValues(array); +>values : IntegerTypedArray +>crypto.getRandomValues(array) : IntegerTypedArray +>crypto.getRandomValues : (array: T) => T +>crypto : Crypto +>getRandomValues : (array: T) => T +>array : IntegerTypedArray + +values.BYTES_PER_ELEMENT; +>values.BYTES_PER_ELEMENT : number +>values : IntegerTypedArray +>BYTES_PER_ELEMENT : number + +declare const integers: IntegerTypedArray; +>integers : IntegerTypedArray + +const ints: IntegerTypedArrayTypes["Int32Array"] = new Int32Array(1); +>ints : Int32Array +>new Int32Array(1) : Int32Array +>Int32Array : Int32ArrayConstructor +>1 : 1 + diff --git a/tsc/testdata/tests/cases/compiler/integerTypedArray.ts b/tsc/testdata/tests/cases/compiler/integerTypedArray.ts new file mode 100644 index 0000000000000..01c0f15a14b7f --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/integerTypedArray.ts @@ -0,0 +1,18 @@ +// @strict: true +// @target: esnext +// @lib: esnext,dom + +crypto.getRandomValues(new Uint8Array(1)); +crypto.getRandomValues(new BigInt64Array(1)); +crypto.getRandomValues(new Float64Array(1)); +crypto.getRandomValues(null); + +declare const buffer: Uint8Array; +crypto.getRandomValues(buffer); + +declare const array: Parameters[0]; +const values = crypto.getRandomValues(array); +values.BYTES_PER_ELEMENT; + +declare const integers: IntegerTypedArray; +const ints: IntegerTypedArrayTypes["Int32Array"] = new Int32Array(1);