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

computed and ComputedGetter are not type-safe #5692

Closed
unshame opened this issue Apr 10, 2022 · 0 comments
Closed

computed and ComputedGetter are not type-safe #5692

unshame opened this issue Apr 10, 2022 · 0 comments
Labels

Comments

@unshame
Copy link

unshame commented Apr 10, 2022

Version

3.2.31

Reproduction link

www.typescriptlang.org/play

Steps to reproduce

  1. Import computed from vue
  2. Create a function that takes an argument
  3. Pass this function directly to computed
  4. Compile the code with typescript
import { computed } from 'vue';

function takes(a: number): string {
    return a.toString();
};

const shouldFail = computed(takes); // works, should fail
const fails = computed(() => takes()); // fails as it should

What is expected?

Typescript should fail to compile with the error "Expected x arguments, but got 0."

What is actually happening?

Typescript compiles the code without an error. Code crashes at runtime.


The problem is that ComputedGetter, which is the first argument to computed, has the type

export declare type ComputedGetter<T> = (...args: any[]) => T;

while it should have the type

ComputedGetter<T> = () => T;

You can pass the function as () => func() to ensure that if an argument is added to the function, typescript will tell you that it is missing, but that seems unnecessarily verbose.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants