How can i "mimic" a table using typing?

Hello there!
I’m trying to find a new way to mimic tables with typing (like adding a new value to the typechecks once the code assigns something to the table)
I remember being possible to do that with something like function abs.Setup<T>(Name: string): T, but suddenly it doesn’t work for me anymore!
using function abs.Setup<T>(Name: string, _Orig: T): T does work, but having to manually add a empty table into _Orig is annoying.
can anyone help me?

1 Like

if you mean how to structure a typecheck for a table, you could do so:

type sometable = {
    SomeStringParam: string,
    SomeNumberParam: number,
    SomeBoolParam: boolean,
    SomeOptionalStringParam: string?,
    SomeVoidFunctionParam: () -> (),
    SomeAnyParam: any,
}

if you want a typecheck for a table that will contain certain data types:

type Dict = {
    [string] = any,
}
type Array = {
    [number] = any,
}

On a side note, the generics < T > you’ve tried to use are used for auto-filling/reusing and morphing the typing of the parameters you pass in: Type checking - Luau

i already know about those and i am already familiar with generic typing
what i’ve wondered about is why it suddenly stopped being auto-expansible, at least for me, some time ago i could assign a key to it and it would add that key to the autocompletion, idk if its just bugged for me but it doesnt anymore

Look at the Luau documentation I’ve linked. Might help your case

Just an add-on for this, if you ever didn’t know.

You can specify fixed string keys alongside the variant keys and it’ll work. I.E.

type Currencies = {
    [string] = number,
    FreeCurrency = number,
    PaidCurrency = number
}

Why to use this: If you have something like events, you may want to specify fixed events that exist and are finalised during development, but there can be many more events that aren’t strictly-typed yet due to development. This is done to silence the type-checker and let it know that “hey, yes, this can exist, and should so long as the variant key type exists.”

	-- [Events] - A list of Signals that are used for
	Events : { 
		[string] : Signal<...any>,
		OnVirtualUserAdded : Signal<VirtualUserAgent, VirtualUser>,
		OnVirtualUserRemoving : Signal<VirtualUserAgent, VirtualUser>
	},

Yes I know, thanks. In my post I’ve only specified cases with tables without variant keys.

You’re talking about ‘unsealed’ table types. For table definitions within a lexical scope, the type is inferred based on changes made to their shape.

local Table = {}
-- Table: {}

Table.Value = 15
-- Table: {Value: number}

function Table.Function() end
-- Table: {Value: number, Function: () -> ()}

The table type gets ‘sealed’ once it leaves scope, for example when returning the type from a function or module. Once it’s sealed, changes to its shape are a type error.

It sounds like either you’re accidently sealing the table, or you’ve ran into an inference bug. Can you provide a reproduction of what’s happening?