You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Define a generic base class Foo with a method function which returns the generic type
Inherit from the base class and specify the generic type
Use the method and expect the specified generic type on the output
---@classFoo<T>---@fieldmyValueTlocalbase= {}
---@returnTfunctionbase:get()
returnself.myValueend---@typeFoo<number>localex1=base--Use class directly---@classExample2:Foo<string>localex2=ex1--Start with a version with generic type specified---@classExample3:Foo<boolean>localex3=base--Start with a version without the generic type specifiedlocalI=ex1.myValue--Correct: numberlocala=ex1:get() --Correct: numberlocalII=ex2.myValue--Correct: stringlocalb=ex2:get() --Correct: stringlocalIII=ex3.myValue--Correct: booleanlocalc=ex3:get() --Correct: boolean
Actual Behaviour
Attempting to expand the parent class directly displays a warning, (Example 3) but using a variable with specified type does not (Example 2).
The method defined in the parent class fails to infer the generic type from the child classes
---@classFoo<T>---@fieldmyValueTlocalbase= {}
---@returnTfunctionbase:get()
returnself.myValueend---@typeFoo<number>localex1=base---@classExample2:Foo<string>localex2=ex1---@classExample3:Foo<boolean>localex3=base--Cannot assign 'Foo' to 'Example3'localI=ex1.myValue--Correct: numberlocala=ex1:get() --Correct: numberlocalII=ex2.myValue--Correct: stringlocalb=ex2:get() --Incorrect: TlocalIII=ex3.myValue--Correct: booleanlocalc=ex3:get() --Incorrect: T
Reproduction steps
Define a generic base class with a method function which returns the generic type
Inherit from the base class and specify the generic type
Use the method and expect the specified generic type on the output
Activity