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

unsound type constraints #8459

Closed
zpdDG4gta8XKpMCd opened this issue May 4, 2016 · 5 comments
Closed

unsound type constraints #8459

zpdDG4gta8XKpMCd opened this issue May 4, 2016 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@zpdDG4gta8XKpMCd
Copy link

zpdDG4gta8XKpMCd commented May 4, 2016

interface I {
    fn: <T>(t: T) => void;
}
interface X {
    x: number;
}
function toI<T extends X>() : I {
    return {
        fn: (t: T) => { // <-- expected a warning from the compiler that the type constraint cannot be enforced
            alert(t.x);
        }
    };
}
function test(i: I): void {
    i.fn({}); // <-- problem unnoticed
}
test(toI<X>()); // displays "undefined"
@zpdDG4gta8XKpMCd zpdDG4gta8XKpMCd changed the title unsound type constrains unsound type constraints May 4, 2016
@malibuzios
Copy link

malibuzios commented May 4, 2016

The example you gave was a bit too complicated for me to understand but I just wanted to mention that assignments of generic functions are not currently type-checked, in general.

Perhaps until the implicit cast of generic function type parameters and return type to any during assignment is addressed, an error can be shown for assignments containing generic function types, in general?

(There's no current error here)

declare let f1: <T>(x: T) => T;
declare let f2: (x: number) => void;

f1 = f2; // Possible error/warning: "generic function types are not 
         // currently type-checked during assignments"

I feel it would be a bit inappropriate to transfer the responsibility to the volunteers who maintain tslint.. Basically to report on an internal implementation issue of the compiler, which may be subject to change in the future, and is not actually related to the language or the design itself.

Edit: corrected "declaration of generic functions" to "assignments of generic functions" and updated the example.

@malibuzios
Copy link

malibuzios commented May 4, 2016

I've updated my comment based on this comment in #3410:

When you call a function with a constrained type parameter, that function's type parameter constraint is checked. When you pass a function with a type parameter constraint, the constraint is not checked.

And this one:

Our assignability relation ignores generics and constraints in signatures. It just replaces all type parameters with any.

@zpdDG4gta8XKpMCd
Copy link
Author

bump

@RyanCavanaugh
Copy link
Member

I believe this is the same root cause as #8397 ?

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 18, 2016
@mhegazy mhegazy closed this as completed Sep 20, 2016
@mhegazy
Copy link
Contributor

mhegazy commented May 18, 2017

tracked by #5616

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants