Skip to content
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

Type 'ArrayBufferView' is not assignable to type 'DataView' #38715

Closed
mhart opened this issue May 21, 2020 · 2 comments
Closed

Type 'ArrayBufferView' is not assignable to type 'DataView' #38715

mhart opened this issue May 21, 2020 · 2 comments
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript
Milestone

Comments

@mhart
Copy link

mhart commented May 21, 2020

TypeScript Version: Nightly

Search Terms:

ArrayBufferView

Expected behavior:

According to MDN, ArrayBufferView is one of these types:

  • Int8Array,
  • Uint8Array,
  • Uint8ClampedArray,
  • Int16Array,
  • Uint16Array,
  • Int32Array,
  • Uint32Array,
  • Float32Array,
  • Float64Array or
  • DataView.

However, in lib.es5.d.ts it's just an interface.

This would be fine if it were used consistently, but it appears that it's used in some places (eg BodyInit) but then in others the actual types from above are listed (eg crypto.subtle.digest). This means, for example, that going from BodyInit to crypto.subtle.digest (while filtering out incompatible types) is impossible.

According to these links:

  1. https://developer.mozilla.org/en-US/docs/Web/API/Request/Request#Syntax
  2. https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#Syntax

This code should compile:

function bodyHash(body: BodyInit) {
    if (typeof body === 'string' ||
        body instanceof Blob ||
        body instanceof FormData ||
        body instanceof URLSearchParams ||
        body instanceof ReadableStream) {
            throw new Error('Only ArrayBuffer or ArrayBufferView allowed')
    }
    // error 2345: Type 'ArrayBufferView' is not assignable to type 'DataView'
    let result = crypto.subtle.digest('SHA-256', body)
}

Actual behavior:

Code fails to compile with type errors

Related Issues:

Possibly #15402

Code

// https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView
type ActualArrayBufferView = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | DataView

function actual(arr: ActualArrayBufferView) {
    // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#Syntax
    // works as it should with no error
    let result = crypto.subtle.digest('SHA-256', arr)
}

function builtin(arr: ArrayBufferView) {
    // error 2345: Type 'ArrayBufferView' is missing the following properties from type 'DataView': getFloat32, getFloat64, getInt8, getInt16, and 13 more
    actual(arr)

    // error 2345: Type 'ArrayBufferView' is not assignable to type 'DataView'
    let result = crypto.subtle.digest('SHA-256', arr)
}

// Real-world example
function bodyHash(body: BodyInit) {
    if (typeof body === 'string' ||
        body instanceof Blob ||
        body instanceof FormData ||
        body instanceof URLSearchParams ||
        body instanceof ReadableStream) {
            throw new Error('Only ArrayBuffer or ArrayBufferView allowed')
    }
    // error 2345: Type 'ArrayBufferView' is not assignable to type 'DataView'
    let result = crypto.subtle.digest('SHA-256', body)
}
Output
"use strict";
function actual(arr) {
    // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#Syntax
    // works as it should with no error
    let result = crypto.subtle.digest('SHA-256', arr);
}
function builtin(arr) {
    // error 2345: Type 'ArrayBufferView' is missing the following properties from type 'DataView': getFloat32, getFloat64, getInt8, getInt16, and 13 more
    actual(arr);
    // error 2345: Type 'ArrayBufferView' is not assignable to type 'DataView'
    let result = crypto.subtle.digest('SHA-256', arr);
}
// Real-world example
function bodyHash(body) {
    if (typeof body === 'string' ||
        body instanceof Blob ||
        body instanceof FormData ||
        body instanceof URLSearchParams ||
        body instanceof ReadableStream) {
        throw new Error('Only ArrayBuffer or ArrayBufferView allowed');
    }
    // error 2345: Type 'ArrayBufferView' is not assignable to type 'DataView'
    let result = crypto.subtle.digest('SHA-256', body);
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jun 10, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jun 10, 2020
@RyanCavanaugh RyanCavanaugh added the Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript label Jun 10, 2020
@mazerab
Copy link

mazerab commented Nov 25, 2020

Hello, I am running into this exact issue and was wondering if there is any workaround one could apply. Thank you, Bastien

@jakebailey
Copy link
Member

This was fixed in #44684, and is in TS 4.4+.

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
Projects
None yet
Development

No branches or pull requests

4 participants