Skip to content

Type of CryptoKey.algorithm is incomplete, making usage of SubtleCrypto difficult #21609

@LiraNuna

Description

@LiraNuna

Search Terms:

cryptokey algorithm

Code

function sign(key: CryptoKey, message: ArrayBuffer): Promise<ArrayBuffer> {
    return crypto.subtle.sign(
        key.algorithm,
        key,
        message,
    );
}

Expected behavior:
CryptoKey.algorithm is an appropriate way to pass the key algorithm to subtle methods such as sign and encrypt. However, the original definition of the type CryptoKey defines the type of algorithm as KeyAlgorithm which misses extra attributes that could be there.

Therefor the expected behavior is for the code to now have a type error and compile correctly. Maybe the correct type for CryptoKey should be

interface CryptoKey {
    readonly algorithm: RsaKeyAlgorithm | EcKeyAlgorithm | AesKeyAlgorithm | HmacKeyAlgorithm | DhKeyAlgorithm;
    readonly extractable: boolean;
    readonly type: string;
    readonly usages: string[];
}

I am not sure how to fully test such a change, since modifying lib.es6.d.ts locally and running tsc produces the same message.

Actual behavior:
The following type error:

Argument of type 'KeyAlgorithm' is not assignable to parameter of type 'string | RsaPssParams | EcdsaParams | AesCmacParams'.
  Type 'KeyAlgorithm' is not assignable to type 'AesCmacParams'.
  Property 'length' is missing in type 'KeyAlgorithm'. (2345)

Playground Link: Playground link

Related Issues: Unknown

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions