Skip to content

Readonly<Float32Array> not assignable to Float32Array #31253

Closed
@chrisui

Description

@chrisui

TypeScript Version: 3.4.0-dev.201xxxxx

Search Terms:

Code

function update(b: Readonly<Float32Array>) {
    const c = copy(b);
    add(c, c);
}

function add(a: Float32Array, b: Float32Array, c: Float32Array = a) {
    c[0] = a[0] + b[0];
}

function copy(a: Float32Array) {
    return new Float32Array(a);
}

Expected behavior:

No error.

Though I would expect not to be able to pass a Readonly value to a function which can mutate it but that's another issue.

Actual behavior:

Error on line 2:

Argument of type 'Readonly<Float32Array>' is not assignable to parameter of type 'Float32Array'.
  Type 'Readonly<Float32Array>' is missing the following properties from type 'Float32Array': [Symbol.iterator], [Symbol.toStringTag]

Playground Link:

See in playground

Note I can workaround the issue by using mappable types on the function arguments but this is pretty verbose and not something I will want to litter a codebase with - Also not going to work with third-party libraries. See that here. Edit: Actually seen another way to do it which is much less verbose here. This however loops again back to this issue since it really should be raising an error (if in strict mode).

Related Issues:

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Mapped TypesThe issue relates to mapped types

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions