Skip to content

() => string and (): string don't match type #44098

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
SephReed opened this issue May 14, 2021 · 4 comments
Closed

() => string and (): string don't match type #44098

SephReed opened this issue May 14, 2021 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@SephReed
Copy link

SephReed commented May 14, 2021

Bug Report

Playground Link

const foo = () => "foo";
const bar: () => string = foo as Omit<typeof foo, "baz">;

Type 'Omit<() => string, "baz">' is not assignable to type '() => string'.
Type 'Omit<() => string, "baz">' provides no match for the signature '(): string'.

🔎 Search Terms

  • () => string and (): string
  • Omit breaks function type match

🙁 Actual behavior

A () => string has a non-existent property omitted can no longer be assigned to () => string

🙂 Expected behavior

It's the same exact type, nothing has changed by omitting a non-existent property.

Actual use case

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgJIGEAWdTIN4BQyxyAFAJQBcyAzmFKAOYDcRJkdF1dDILbxeHWgB+LrXpNWAXwIEEAGzg0ayLDhAAJAPYKAJtHwDkABwCuAIwXAEyRhDDIE2UBSMkPT7SDrJtZsGoMFxBkAF4yNzCAPmQAIhhtbTjyZGU0kABPVk8SKAczKFD-R3SAeQBbYDAAHjBMkwhtGD8AgBp4oUgoOOickllZeW9fTF0DKHDkEAgAdzUQnX1oClYx5agAOmcNVeIAen3kCzg9ZGgobSgCdYntkM2ulfJmZEO7JLOLq5vx6HuNJsOGBVgQgA

@MartinJohns
Copy link
Contributor

Possibly the same as #44089?

@DanielRosenwasser
Copy link
Member

Mapped types like Omit drop signatures from object types. As far as I can tell, this is working as intended.

@SephReed
Copy link
Author

@MartinJohns maybe... they seem like they could stem from somewhere similar, though they aren't the same.

@DanielRosenwasser if this is working as intended, is there any way of omitting a property from a function with it remaining a function?

So far, the best I've found is to just re-add the function signature:

const foo = () => "foo";
const bar: () => string = foo as Omit<typeof foo, "baz">;  // doesn't work
const qux: () => string = foo as (Omit<typeof foo, "baz"> & (() => string));  // works

@tjjfvi
Copy link
Contributor

tjjfvi commented May 14, 2021

if this is working as intended, is there any way of omitting a property from a function with it remaining a function?

You can use something like

type OmitWithFn<T extends (...args: any) => any, K> = Omit<T, K> & ((...args: Parameters<T>) => ReturnType<T>)

But if you need overload or generic support, you'll need to manually re-add it.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label May 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants