Type Checking any GENARIC Table

Like fr… how the HELL do you type like… ANY Table… Like a list, a dictionary and nested lists and dictionaries, so that the new type solver knows what you mean. It would also be nice if i could use Generics like this:

type TableType<E, K> = *idk wth too put here... :/*
--    Element  ^  ^ Key

Like i tried this but…
image


ya… Tbh I’m to tired rn to figure this out… help would be greatly appreciated… :sob:

1 Like

I’m not extremely experienced in type checking but this works for dictionaries.

type coolDictionary = {
	Property: string,
	Method: () -> ()
}
1 Like

You DO know I mean like ANY table as in:

local t1: TableLike = {1, "foo", 3, {"another", "table"}}
local t2: TableLike = {
    hi: "1"
    test: 100
    [7] = {1, "foo", Instance.new("Part")},
    [Instance.new("Folder")] = {
         ["not"]: "again"
    }
}

With those all being valid with the type. I don’t want Static types like that.

Sorry, I don’t know why I thought you meant that.

I know you’ve probably already read this, but here’s the section on generics if you haven’t seen this yet:

Maybe the problem is with the cyclic referencing of the type? You defined it as:

  1. Being a table with an index of K that has a value type of E; OR itself (I don’t know how that would work)
  2. Or, being a table with no index that has a value type of E; OR itself (again, I don’t know how that would work)

Why can’t you just define it as:

export type TableLike<K, E> = {[K]: E}

Shouldn’t it be l[1], l[2], etc. instead of l["string"]? Your definition of TableType takes the element first and key second; not the other way around.

Any as in like any?

type TableType<K = any, E = any> = { [K]: E }
local l: TableType = {}
--[[
local l: {
    [any]: any
}
]]

print(l["any"]) -- nil

As long as you don’t care about the specifics of the type {[any]: any} might be what you’re looking for.

function AcceptsAnyTable(x: {[any]: any})
	--You can still test for indexers, such as the following:
	local y = x[1]
	local z = x["key"]
end

--All the following calls are fine under strict type checking:
AcceptsAnyTable({})
AcceptsAnyTable({1, 2, 3, 4, 5})
AcceptsAnyTable({1, "two", 3, "four", Color3.new(5, 0, 0)})
AcceptsAnyTable({
	key1 = {
		v1 = 1,
		v2 = 2
	},
	key2 = "value"
})

Let me know if this doesn’t work under a particular circumstance or you’re looking for some other behavior.