-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Intersection with void should resolve to never #55700
Comments
This is not really accurate. |
for the majority of cases, the implicit-return is the typical use case of function foo(): void {
console.log('foo')
} Which implicitly returns undefined. And for
at the type level it still means "nothing" (or "absence of any type information"), thus resolving |
This issue spawn from my work of the The generalized version of it (which accepts any types, including special types like const x = { a: 1 }
const y = undefined
const z = { ...x, ...y } works, |
Every time I think about For function return types it's essentially The current behavior has to be intentional though, so whatever it is, it's not a bug. |
|
I think that is a bit too pessimistic. The function return type is Also, I'm not suggesting we should change the function return type from I'm strictly talking about the behavior of intersecting with void, i.e. If that is a specific reason for that, yeah that's fine. But I suspect it's something legacy and does not apply today anymore as TypeScript has evolved and the team has better understanding on how it should behave. |
This is pretty much exactly how the type system operates; it reasons about things based on what’s possible for the types involved and further constraints implied by the specific form of a declaration never come into play. |
Agree but disagree. So while in theory you are correct, at the same time we are not talking about an imagined type system in isolation. I think a lot of the discussion here is around function signature assignability. i.e.: const f: () => void = () => 1 Which could be a feature by itself and I agree that changing that would break a lot of code. But here I'm really try to clarify and describe what PS: TBH, I think that behavior is also legacy and most likely because back then there is no My guess is if the TypeScript team could do this again, such assignment should be prohibited. |
If |
I'm not sure I follow the logic. Can you explain? How does The former is about assignability on that specific form. For example, https://www.typescriptlang.org/play#code/MYewdgzgLgBAngLhgNxASwCYwLwwN4wCGSA5IQEbAkwC+AsAFBA the same goes for: const f: () => void = () => ({ x: "hello " })
let r = f()
r = { x: 'hello' } // invalid as `r: void` Note that this is also invalid: // Type '{ x: string; }' is not assignable to type 'void & { x: string; }'.
const f: () => void & { x: string } = () => ({ x: 'abc' }) Also, what exact does For example, let y: boolean & { x: string } = Object.assign(true, { x: 'abc' })
console.log(y.valueOf()) // true
console.log(y.x) // 'abc' In contrast, let y: void & { x: string } = ??? // what can be assigned to it?
y.x // and what can be done with it? |
btw, I understand that the assignment in the above examples is backwards. The assignment logic is really However, the problem I have is the last piece, that the type That's why I argue that Unless there are some actual meaningful use case or example of |
I don’t know if this has been linked yet but if not: #42709 In particular, this remark from there is in line with what you’re saying:
|
The way I think of it is that declare let s1: (() => void) & (() => { x: string })
declare let s2: () => void & { x: string };
declare let t: () => { x: string };
t = s1;
t = s2;
t().x; Is it useful? Not that I can tell. Is it weird? Yes. But is it about as internally consistent as it can be given the weird rules? I think so. |
@fatcerberus thanks for linking to #42709. It is very comprehensive. I think @andrewbranch thank you for the example. I am thinking that since For example, by doing this, type-level do not need to perform any special handling of |
I would generally like to see |
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
🔎 Search Terms
intersection, void
🕗 Version & Regression Information
void
⏯ Playground Link
https://www.typescriptlang.org/play?#code/C4TwDgpgBAmlC8UDeUCGAuKBnYAnAlgHYDmUAvlAGRQBuA9vgCYBQQA
💻 Code
void
is a more restricted type ofundefined
specifically to describe function that "returns nothing".In the JavaScript land, it actually returns an implicit
undefined
.So for all due and purposes, I believe
void
should have similar behavior asundefined
whenever possible.For
undefined
, intersection type resolves tonever
https://www.typescriptlang.org/play?ssl=1&ssc=44&pln=1&pc=1#code/C4TwDgpgBAmlC8UDeUCGAuKBnYAnAlgHYDmUAvlAGRQCuhAJhAGZET1QD0HUhEAbhFwAoIA
🙁 Actual behavior
🙂 Expected behavior
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: