Skip to content

Bug? Closure arguments not checked for variable number of arguments #2506

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
mindplay-dk opened this issue Mar 26, 2015 · 2 comments
Closed
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@mindplay-dk
Copy link

Example:

class Biff {}
class Bang {}

interface Callback {
    (...biffs: Biff[]);
}

function foo(callback: Callback) {
    callback(new Biff(), new Biff());
}

foo((x: Biff, y: Bang) => console.log('whoa'));

foo((z: string, n: number) => console.log('huh'));

The closures passed as arguments in the two calls at the end, do not appear to get type-checked.

It's pretty plain that these closures will not be invoked with suitable arguments, yet the code appears to compile with no complaints.

How come this doesn't get type-checked?

@ivogabe
Copy link
Contributor

ivogabe commented Mar 26, 2015

Types are checked, but TypeScript has a structural type system. That means that some value is assignable to a type if it has (at least) all the properties of that type. Thus an object that has at least 0 properties is assignable to Biff. Every object has at least 0 properties, so the type Biff is basically equal to any. If you change Biff to:

class Biff {
  someProperty: number;
}

You will get errors.

@mindplay-dk
Copy link
Author

Right, so even string and number are assignable to a type with an empty interface - yeah, I forget that's how this works and get confused sometimes because the type system syntax is so similar to other languages where this is not the case. Thanks!

@danquirk danquirk added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Mar 26, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

3 participants