Repository navigation
Contextually type class property initializers by extends/implements types #3667
Description
Activity
- addedSuggestionAn idea for TypeScriptAn idea for TypeScript
on Jun 29, 2015 Is this not the same as #1373?
RyanCavanaugh commented
on Jun 29, 2015 MemberAuthorMore actions#1373 is about methods, this is about properties
DanielRosenwasser commented
on Jun 29, 2015 MemberMore actionsSo is this a broader proposal than #1373? In other words, in the following example, is the arrow function in
SortByQueryIdcontextually typed?Additionally, if we declared
getvalueinISortInfoas a property but defined it as a method in the class, would its parameter's type be inferred?export interface ISortInfo { getvalue(x: QuerySummary): number | string; order: SortDir; ordercalc?: number; } class SortByQueryId implements ISortInfo { getvalue = x => x.QueryId; order = SortDir.Flip; }
RyanCavanaugh commented
on Jun 29, 2015 MemberAuthorMore actionsI don't think we should distinguish between method-style and property-style declarations in the interface - both are an equally valid "source" in terms of type information, regardless of how the class is implementing it.
In the example, I would expect the arrow function to be contextually typed under this proposal, but not #1373.
sophiajt commented
on Jul 16, 2015 ContributorMore actionsAgree with Noel Abrahams (@NoelAbrahams) - can we merge this with #1373? If this subsumes that proposal, can we dupe that one to this one?
Also merge with #3804 ? Then we include interfaces too.
👍
From #5181 the following sample reproduces this issue:
declare module Backbone { interface RoutesHash { [routePattern: string]: string | {(...urlParts: string[]): void}; } class Events { } class Router extends Events { routes: RoutesHash; } } declare module Marionette { class AppRouter extends Backbone.Router { } } module Backbone.Tests { // Error, routes is not contextually typed class MyRouter extends Backbone.Router { routes = { "some/route": "someMethod", "some/otherRoute": () => {} }; } } module Marionette.Tests { // Error, routes is not contextually typed class MyRouter extends Marionette.AppRouter { routes = { "some/route": "someMethod", "some/otherRoute": () => {} }; } }
- addedIn DiscussionNot yet reached consensusNot yet reached consensus
on Oct 8, 2015 - addedCommittedThe team has roadmapped this issueThe team has roadmapped this issueand removedIn DiscussionNot yet reached consensusNot yet reached consensus
on Nov 3, 2015 RyanCavanaugh commented
on Nov 3, 2015 MemberAuthorMore actionsTentatively approved -- this would be a slight breaking change, but probably very much in the "good" direction (i.e. uncovers bugs that were going missed before). We will investigate breakages in our RWC suite to see what the net impact is.
I'd be fine with this breaking change.
Unfortunately, we couldn't come up with a solution that was both consistent and backward-compatible. The breaks in our Real World Code suite were more bad than good. See #6118 for details. I'm closing this for now.
Reacted by Jeremy Chone- addedDesign LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixedWon't FixThe severity and priority of this issue do not warrant the time or complexity needed to fix itThe severity and priority of this issue do not warrant the time or complexity needed to fix it
on May 6, 2016 - locked and limited conversation to collaborators
on Jun 19, 2018
When a property has exactly one type from an
extendsorimplementsclause, we should contextually type that property's initializer by the type from the clause. For example:Ideally we could take this one step further when widening. The following behavior is also undesirable:
e.g. #3666