Skip to content

Combining generic classes and variadics #787

Closed

Description

@Polanas

I've been trying to write a function that accepts a number of Wrapped<T>'s and returns T's, but couldn't get it to work.

---@class Wrapper<T>

---@generic T
---@param ... Wrapper<T>...
---@return T...
function unwrap(...) end

---@type Wrapper<int>, Wrapper<string>
local a, b

--should be int, string
local b, c = unwrap(a, b)

Instead a warning is thrown: expected 0 parameters but found 2 [redundant-parameter].
Can something like this be done with current variadics implementation?

Activity

  1. changed the title [-]Combning Classes with Generic Parameters and Variadics[/-] [+]Combining Classes with Generic Parameters and Variadics[/+] on Oct 2, 2025
  2. changed the title [-]Combining Classes with Generic Parameters and Variadics[/-] [+]Combining generic classes and variadics[/+] on Oct 2, 2025
  3. CppCXY commented on Oct 3, 2025

    @CppCXY
    Member

    This form is not supported yet; it is probably related to issue #714.

  4. xuhuanzy commented on Oct 3, 2025

    @xuhuanzy
    Member

    I am currently implementing generic type inference similar to TypeScript, and I have already implemented 'infer'. I believe that type inference will be achievable by then.
    #786

  5. xuhuanzy commented on Oct 3, 2025

    @xuhuanzy
    Member

    The corresponding code in TypeScript

    type Wrapped<T> = T;
    
    type UnwrapUnion<T> =
        { [K in keyof T]: T[K] extends Wrapped<infer U> ? U : never };
    
    function unwrapUnion<T extends Wrapped<unknown>[]>(
        ...items: T
    ): UnwrapUnion<T> {
    }
    
    const w1: number = 1;
    const w2: number = 1;
    const w3: boolean = true;
    
    const result = unwrapUnion(w1, w2, w3);
  6. Polanas commented on Oct 3, 2025

    @Polanas
    Author

    I am currently implementing generic type inference similar to TypeScript, and I have already implemented 'infer'. I believe that type inference will be achievable by then.
    #786

    That's amazing! Thank you for your efforts. Lua with a type system as powerful as TS's sounds like a dream.

  7. added a commit that references this issue on Oct 5, 2025
    f9275cf
  8. linked a pull request that will close this issueGenerics #786on Oct 5, 2025
  9. xuhuanzy commented on Oct 5, 2025

    @xuhuanzy
    Member

    Provides generic type inference implementation
    Image

  10. added a commit that references this issue on Oct 9, 2025
    7f1f4c9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions