Skip to content

Readonly<Float32Array> not assignable to Float32Array #31253

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

Closed
chrisui opened this issue May 4, 2019 · 7 comments · Fixed by #42543
Closed

Readonly<Float32Array> not assignable to Float32Array #31253

chrisui opened this issue May 4, 2019 · 7 comments · Fixed by #42543
Labels
Bug A bug in TypeScript Domain: Mapped Types The issue relates to mapped types
Milestone

Comments

@chrisui
Copy link

chrisui commented May 4, 2019

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

@ExE-Boss
Copy link
Contributor

ExE-Boss commented May 6, 2019

This is caused by the fact that symbols aren’t preserved by mapped types.

@weswigham weswigham added Bug A bug in TypeScript Domain: Mapped Types The issue relates to mapped types labels May 8, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.6.0 milestone Jun 12, 2019
@andrewbranch
Copy link
Member

This is caused by the fact that symbols aren’t preserved by mapped types.

Yep, or more specifically, they’re not exposed by keyof. I’m trying to dig up why that is, but changing it would likely be a huge breaking change... @weswigham what are your thoughts here?

@weswigham
Copy link
Member

weswigham commented Jun 25, 2019

Mapped types do retain symbol keys - Certain builtin symbols (ie, Symbol.iterator) we know of predate unique symbols and do not appear in any object key lists - #24738 was originally opened to address this, and typesVersions was made to make the migration to real unique symbols for these global symbols better.

@andrewbranch
Copy link
Member

Got it. In a sense, then, this is a duplicate of #24622. At least, I think it’s blocked on #24738.

@Jamesernator
Copy link

Certain builtin symbols (ie, Symbol.iterator) we know of predate unique symbols and do not appear in any object key lists

Glad to see this is being fixed, just found out that this does not currently work:

const asyncIterable: AsyncIterable<number> = Object.freeze({
  * [Symbol.asyncIterator]() {
    yield 1
    yield 2
  },
})

@andrewbranch
Copy link
Member

@DanielRosenwasser @RyanCavanaugh this is in the 3.6.1 milestone, but I don’t think it’s actionable without #24738, which, uh, probably cannot go into 3.6.

@andrewbranch andrewbranch modified the milestones: TypeScript 3.6.1, Backlog Sep 4, 2019
@andrewbranch andrewbranch removed their assignment Sep 4, 2019
@thecotne
Copy link

thecotne commented Mar 24, 2020

@weswigham can you please explain how is this a bug?

it seems that if copy wants a mutable Float32Array and you give it an immutable (readonly) Float32Array it's defiantly a mistake because you can't modify immutable data but if you wanted mutable data then you probably will try to mutate it (if you have no plans to mutate data you can change type annotation to be immutable (readonly) like so Readonly<Float32Array> and it will work fine)


edit

this part is a bug Type 'Readonly<Float32Array>' is missing the following properties from type 'Float32Array': [Symbol.iterator], [Symbol.toStringTag]

my mistake ...

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: Mapped Types The issue relates to mapped types
Projects
None yet
8 participants