mirror of
https://github.com/luau-lang/rfcs.git
synced 2025-05-04 10:43:48 +01:00
Added more clarifications on interactions of index and classes
This commit is contained in:
parent
202e9ddc31
commit
1f61d7346b
1 changed files with 9 additions and 1 deletions
|
@ -76,7 +76,15 @@ type idxType3 = index<Person | Person2, "age"> -- idxType3 = number | string
|
||||||
type idxType4 = index<Person | Person2, "alive" | "age"> -- Error message: Property '"age" | "alive"' does not exist on type 'Person | Person2'
|
type idxType4 = index<Person | Person2, "alive" | "age"> -- Error message: Property '"age" | "alive"' does not exist on type 'Person | Person2'
|
||||||
```
|
```
|
||||||
|
|
||||||
In the circumstance that the indexee is a type class or table with an `__index` metamethod, `index` will *only* invoke `__index` if indexer is not found within the current scope.
|
In the circumstance that the indexee is a type class or table with an `__index` metamethod, the `__index` metamethod will *only* be invoked if the indexer is not found within the current scope:
|
||||||
|
```lua
|
||||||
|
local exampleClass = { Foo = "eight" }
|
||||||
|
local exampleClass2 = setmetatable({ Foo = 8 }, { __index = exampleClass })
|
||||||
|
local exampleClass3 = setmetatable({ Bar = "nine" }, { __index = exampleClass })
|
||||||
|
|
||||||
|
type exampleTy2 = index<typeof(exampleClass2), "Foo"> -- exampleTy2 = number
|
||||||
|
type exampleTy3 = index<typeof(exampleClass3), "Foo"> -- exampleTy3 = string
|
||||||
|
```
|
||||||
|
|
||||||
Implementation is straight forward: the type of the indexee will be determined (table, class, etc) -> search through the properties of the indexee and reduce to the corresponding type of the indexer if it exists; otherwise, reduce to an error.
|
Implementation is straight forward: the type of the indexee will be determined (table, class, etc) -> search through the properties of the indexee and reduce to the corresponding type of the indexer if it exists; otherwise, reduce to an error.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue