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
{{ message }}
Repository navigation
Allow value pointing to a class or whose type has a new signature to be used as a type #45013
When a class is created by a static method, there doesn't seem to be a way to directly use it as a class type.
📃 Motivating Example
classBaseClass{constructor(){}staticcloneClass(){returnclassextendsthis{};}}functiontakeBase(base: BaseClass){}// OktakeBase(newBaseClass());// OkconstCreatedClass=BaseClass.cloneClass();functiontakeCreated(created: CreatedClass){}// ~~~~~~~~~~~~// 'CreatedClass' refers to a value, but is being used as a type here.// Did you mean 'typeof CreatedClass'?takeCreated(newCreatedClass());// Ok
While CreatedClass is indeed a value, it's a value that happens to be equal to a class.
💻 Use Cases
In general, class factories like this are rather unwieldy to set up in the type system.
See this playground link for a version that uses an interface to describe the class, and this playground link for a verson that purely uses a static class. The former would be resolved nicely by this issue's requested feature; the latter would be doable in a more complex way if #26242 and/or #5863 are implemented.
Martin Johns (@MartinJohns) true, but that's got pretty bad ergonomics. See the linked issue -- library authors shouldn't have to force the constituents into using type shenanigans like that.
I really don't see how this is "type shenanigans". It's just how the type system is designed and works. And I'd say const variables automatically being imported to the types would be even worse ergonomics.
Name resolution isn't allowed to have fallback behavior. Doing this creates a semantic trap where adding a new top-level type possibly changes the meaning of existing code. And obviously we can't change the behavior to prefer an inner-scoped const over an outer-scoped interface; this would be breaking beyond measure.
Activity
MartinJohns commentedon Jul 13, 2021
But you can refer to it using the
typeofoperator. This works just fine:JoshuaKGoldberg commentedon Jul 13, 2021
Martin Johns (@MartinJohns) true, but that's got pretty bad ergonomics. See the linked issue -- library authors shouldn't have to force the constituents into using type shenanigans like that.
MartinJohns commentedon Jul 13, 2021
I really don't see how this is "type shenanigans". It's just how the type system is designed and works. And I'd say
constvariables automatically being imported to the types would be even worse ergonomics.But const variables not introducing new types is the intended behavior.
Your feature request goes in the same direction as #44681.
RyanCavanaugh commentedon Jul 13, 2021
Name resolution isn't allowed to have fallback behavior. Doing this creates a semantic trap where adding a new top-level type possibly changes the meaning of existing code. And obviously we can't change the behavior to prefer an inner-scoped
constover an outer-scopedinterface; this would be breaking beyond measure.typescript-bot commentedon Jul 17, 2021
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.