Skip to content

Target signature provides too few arguments. Expected 1 or more, but got 0 #58927

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
seafronthu opened this issue Jun 19, 2024 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@seafronthu
Copy link

seafronthu commented Jun 19, 2024

🔎 Search Terms

ts(2345)

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions 5.4.5__ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

No response

💻 Code

interface RegisterFnDepINF  {
  close: Array<() => void>
  error:  Array<() => void>
  message: Array<(msg: string) => void>
  open:  Array<() => void>
}
type EventType = keyof RegisterFnDepINF
class WebSocketCommunication {
  #registerFnDep: RegisterFnDepINF = {
      close: [],
      error: [],
      message: [],
      open: []
    }
    $on<T extends EventType>(type: T, callback:  RegisterFnDepINF[T][number]) {
      this.#registerFnDep[type].push(callback)
    }
// errors
// Argument of type '(() => void) | (() => void) | ((msg: string) => void) | (() => void)' is not assignable to parameter of type '() => void'.Type '(msg: string) => void' is not assignable to type '() => void'.Target signature provides too few arguments. Expected 1 or more, but got 0.
}

🙁 Actual behavior

Argument of type '(() => void) | (() => void) | (() => void) | ((msg: string) => void)' is not assignable to parameter of type '() => void'.
Type '(msg: string) => void' is not assignable to type '() => void'.
Target signature provides too few arguments. Expected 1 or more, but got 0.

🙂 Expected behavior

not error

Additional information about the issue

interface RegisterFnDepINF  {
  close: Array<() => void>
  error:  Array<() => void>
  message: Array<(msg: string) => void>
  open:  Array<() => void>
}
type EventType = keyof RegisterFnDepINF
class WebSocketCommunication {
  #registerFnDep: RegisterFnDepINF = {
      close: [],
      error: [],
      message: [],
      open: []
    }
    $on<T extends EventType>(type: T, callback:  RegisterFnDepINF[T][number]) {
      this.#registerFnDep[type].push(callback)
    }
}

https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgEoQObAM6SgMRABEIAHASQDl9lkBvAKFoQBsB7bCALmQEEoocAJ4AeABQBKZAF4AfMgBubYABNZTZNChsoPPgOHipcxcrUaAthGzY4GbvsGixF7Bh64ooDMflLV6rRspBAgevxORjJ+ZuoAvgxgQiHIAKIKoWAAKsko0sgA1hBCbDBomDh4hCQU1AyscDbIAOoQAEYAymwIRWAAwmwWFgCuIMAIcGDAbCD0GgDEUBW40NVkPOhYKwTEZFQ0+Yy0x8isHA4A2gC6ADQaJ1o6PNd3J7RWNnaXt-fHwaHPK6-BInAAkMxEWU0AA9ICAVNg0hlwDkQrIxEkQjwsjdTnAWCw2ogCnpNpVVrtavgLlkrhcQMMLG1oFcpEc3mAABY4AB0i2WVUpF0xECuPNIw2wnLEEwJRJ6EmBDDiQA
version: 5.4.5

@MartinJohns
Copy link
Contributor

MartinJohns commented Jun 19, 2024

You wrongly assume that T extends EventType means that T can only be one specific key, but it can also be a union of keys. This is very known design limitation.

This is an example that demonstrates the problem:

// Passing type "message", but callback without an argument
$on<"close" | "message">("message", () => {})

You would need #27808 for this.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 20, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants