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

function that takes generic with no constraint can call function with type constraint with invalid type #43956

Closed
DetachHead opened this issue May 5, 2021 · 3 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@DetachHead
Copy link
Contributor

DetachHead commented May 5, 2021

Bug Report

πŸ”Ž Search Terms

Generic no constraint call function with no constraint

πŸ•— Version & Regression Information

4.3.0-dev-20210504

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.3.0-dev.20210506#code/GYVwdgxgLglg9mABAIwIYCcA8AVRBTADyjzABMBnRAbwG0BrPATwC5Fyp0YwBzAXVdRhGAXwB8ACgBuqADYg8rbAEpqAKEQbE0uXgB0AC1TkA8gHcwABXRwADnnRRG4gOTA4cZ0tXDVq0JFgERDc4HAlteUUlKnVNNCxscNl5FQB6VLA4fHRrdAAafEkSRCh9OBBufURcUjg8cjBnKEQAW1QoCCrSvEQIBHZ0VC4ob1948XBSPGAuPFI01L6cvGgZRmzc8lZJ6dnSRBhKTOajchhuMFRkGR6oLOw-dwmyXbA5hcyNuHzgoZlKdqIdDgWAtPBAA

πŸ’» Code

function bar<T extends {[key: string]: any}>(value: T) {
    value.hasOwnProperty('foo')
}

function foo<T>(value: T){
    bar<T>(value) //no error, even though T doesn't match the constraint
}

bar(undefined) //correctly errors: undefined is not assignable to T
foo(undefined) //no error, fails at runtime

πŸ™ Actual behavior

No compile error when calling bar with a type that doesn't match the bounds of T

πŸ™‚ Expected behavior

Compile error

@MartinJohns
Copy link
Contributor

MartinJohns commented May 5, 2021

This sounds somewhat like #31006.

@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels May 5, 2021
@RyanCavanaugh
Copy link
Member

This would be a pretty noisy breaking change for likely not much upside, but we can discuss

@DetachHead
Copy link
Contributor Author

this seems to be fixed as of 4.8

function bar<T extends {[key: string]: any}>(value: T) {
    value.hasOwnProperty('foo')
}

function foo<T>(value: T){
    bar<T>(value) //error in 4.8
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants