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

Support for intersection types (& operator) #119

Open
LoganDark opened this issue Apr 9, 2022 · 5 comments
Open

Support for intersection types (& operator) #119

LoganDark opened this issue Apr 9, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@LoganDark
Copy link

The ability to do @type table<string, string> & number[] to say that it is both a string->string table and an array of numbers

@Benjamin-Dobell
Copy link
Owner

You've got union and intersection types around the wrong way; currently only union types are supported. There are no intersection types.

That said, with the specific example you've given, an equivalent type can be defined as:

---@shape NumberArrayStringMap
---@field [number] number
---@field [string] string

---@type NumberArrayStringMap
local numberArrayStringMap1 = {}

---@type { [number]: number, [string]: string }
local numberArrayStringMap2 = {}

Would like intersection support, however honestly it's probably a way off. Currently it's not yet possible to extend multiple shapes, that'd need to come first.

@LoganDark
Copy link
Author

LoganDark commented Apr 9, 2022

currently only union types are supported. There are no intersection types.

Union types support all operations of all types, intersection types only support operations that both types support. Is that backwards?

That said, with the specific example you've given, an equivalent type can be defined as:

---@shape NumberArrayStringMap
---@field [number] number
---@field [string] string

---@type NumberArrayStringMap
local numberArrayStringMap1 = {}

---@type { [number]: number, [string]: string }
local numberArrayStringMap2 = {}

Thanks!

@LoganDark
Copy link
Author

LoganDark commented Apr 9, 2022

https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#intersection-types

I fucking hate type theory and how everything is backwards, I mean, intersection literally means where two things collide / have in common, but whatever

@Benjamin-Dobell Benjamin-Dobell changed the title Union types (rather than intersection) - & operator Support for intersection types (& operator) Apr 9, 2022
@Benjamin-Dobell Benjamin-Dobell added the enhancement New feature or request label Apr 9, 2022
@Feuermurmel
Copy link

https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#intersection-types

I fucking hate type theory and how everything is backwards, I mean, intersection literally means where two things collide / have in common, but whatever

Yes, that's type theory! 😁 The more options you have in a type, the fewer things you know about its values. Or you can think about it this way: The properties of a type (e.g. what fields you can access) are a function what values are not part of the type (the type's complement, it's all just set theory btw.). The more values you can rule out, the more properties you know are guaranteed. If you look at the complement of types, the intersection of two types become their union and vice-versa.

So if you take the union of what you can rule out in two types, you get the union of the types' properties…


I have another example I've run into where I wanted to use an intersection type. I'm writing type stubs for a library that provides functions inside of tables in global variables. Some of these functions are overloaded, so I can write (without any require):

foo.bar()
foo.bar(42)

To type this, I wanted to write the following declaration (pseudo-syntax) in a stub file (a Lua file that I'm never importing):

---@shape Foo
---@field bar fun() & fun(a: number)

foo = --[[---@type  Foo]] {}

Instead, I managed to get the result I wanted with this:

---@shape Bar
---@overload fun()
---@overload fun(a: number)

---@shape Foo
---@field bar Bar

foo = --[[---@type  Foo]] {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants