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

map.setIfUndefined doesn't consider inheritance in parameters #82

Closed
byrond opened this issue Nov 22, 2023 · 1 comment
Closed

map.setIfUndefined doesn't consider inheritance in parameters #82

byrond opened this issue Nov 22, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@byrond
Copy link

byrond commented Nov 22, 2023

Describe the bug
When we attempt to call map.setIfUndefined(A, 'key', () => new B) where class B extends the class used in Map A, we get the following:

Argument of type 'Map<string, A>' is not assignable to parameter of type 'Map<string, B>'.
  Type 'A' is missing the following properties from type 'B': ... ts(2345)

This seems to be related to its definition:

 * @function
 * @template V,K
 * @template {Map<K,V>} MAP
 * @param {MAP} map
 * @param {K} key
 * @param {function():V} createT
 * @return {V}

We have worked around the issue by copying the logic inline instead of using map.setifUndefined, but it would be great to be able to use it with inherited classes.

To Reproduce
Steps to reproduce the behavior:

Use the following JS, and attempt to generate types with tsc:

import * as map from 'lib0/map'

class A {
  constructor() {
    this.property1 = 1
  }
}

class B extends A {
  constructor() {
    super()
    this.property2 = 2
  }
}

/**
 * @type {Map<string, A>}
 */
const set = new Map()

const item = map.setIfUndefined(set, 'key', () => new B)

Expected behavior
We can call map.setIfUndefined(A, 'key', () => new B) where class B extends the class used in Map A, and the function adds a new instance of B to the map without errors.

Screenshots
If applicable, add screenshots to help explain your problem. Tip: you can drag-drop images into this textarea.

Environment Information

  • Node 18.18.2

Additional context
Add any other context about the problem here.

@byrond byrond added the bug Something isn't working label Nov 22, 2023
@dmonad dmonad closed this as completed in 00fbf0b Nov 22, 2023
@dmonad
Copy link
Owner

dmonad commented Nov 22, 2023

Makes sense. I fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants