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

Intersection types cant be used properly as parameters in other functions #1141

Open
metatablecat opened this issue Jan 7, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@metatablecat
Copy link

metatablecat commented Jan 7, 2024

Assume the following code sample:

type Cat = {meow: (Cat) -> ()}

local function consumer(cat: Cat & {age: number})
	cat:meow()
end

consumer {
	meow = function(self)
		
	end,
	
	age = 5
}

For whatever reason, the self inside the meow definition has no type, where I would expect it to have the type Cat.

image

If I modify the code to remove the intersection and instead place it under one type;

type Cat = {meow: (Cat) -> (), age: number}

local function consumer(cat: Cat)

...the callback will mysteriously gain it's type.

image

This example doesn't really showcase the problem, though this issue more extends to generics where I'm attempting to extend the type of a table with callbacks, to use in another function.

The only place where I'd expect this to be confused over the type is if both sides of the intersection define a different type for the same key, for example:

type TwoSidedFunction = {
  add: (number) -> number
} & {
  add: (number, number) -> number
}
@metatablecat metatablecat added the bug Something isn't working label Jan 7, 2024
@greydoubt
Copy link

the self parameter in the meow function lacks an explicit type due to the intersection
maybe try

type Cat = {meow: (self: Cat) -> (), age: number}
type CatWithAge = Cat & {age: number}

local function consumer(cat: CatWithAge)
cat:meow()
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants