Skip to content

Consider only showing builders in object literal completions its contextual type has an index signature #4089

Closed

Description

Since users are no longer allowed to have excess properties in an object literal, it might be appropriate to only show a builder when the contextual type of an object literal has no index signature.

For instance:

type T = { x: string; y: number; };
function f(param: T) {
}

f({ /*$*/ }); // should only allow 'x' and 'y' and no builder.

However, this may not be sufficient; in the presence of a type assertion, one actually does want to allow a builder, since this is one of the recommended workarounds anyway:

type T = { x: string; y: number; };
function f(param: T) {
}

f(<T>{ /*$*/ }); // should only allow 'x' and 'y' and no builder.

Activity

  1. danquirk commented on Jul 30, 2015

    @danquirk
    Member

    Seems like a good idea

  2. DanielRosenwasser commented on Jul 30, 2015

    @DanielRosenwasser
    MemberAuthor

    Though I recall another edge condition is that we would need to allow a builder for the empty type as well.

  3. JsonFreeman commented on Aug 2, 2015

    @JsonFreeman
    Contributor

    I believe you would also want a builder when the contextual type is from a type assertion, because you are allowed to specify extra properties there as well.

    You may want to look at isKnownProperty in the checker for inspiration.

  4. DanielRosenwasser commented on Aug 2, 2015

    @DanielRosenwasser
    MemberAuthor

    Jason Freeman (@JsonFreeman) sorry that's what I originally meant, I wrote "completion" instead of "a builder" in the original issue. Corrected.

  5. JsonFreeman commented on Aug 2, 2015

    @JsonFreeman
    Contributor

    Yes, but I meant that maybe the builder would always be shown in a type assertion, since type assertions do not view their expressions as fresh.

  6. DanielRosenwasser commented on Aug 8, 2015

    @DanielRosenwasser
    MemberAuthor

    Right, that was what I was getting at. And actually there's another problem - consider the following:

    function f(options: FooOptions): void;
    function f(options: BarOptions): void;
    function f(options: any) { }

    Provided that FooOptions and BarOptions are non-empty and distinct, if you supply an empty object literal to f, you are potentially going to only get BarOptions (since resolution would fail on FooOptions and settle on BarOptions).

    This would be bad experience. We should either supply properties from both FooOptions and BarOptions or not force completion.

  7. JsonFreeman commented on Aug 8, 2015

    @JsonFreeman
    Contributor

    I think that is a different issue. You'd need to detect whether multiple overloads are involved in obtaining the contextual type, and decide on a policy for this case. I think it may be acceptable to pick one and show a builder. Alternatively, you could try to collect them from all the overloads, but I think that would be complicated, and not that useful. I would also try to have completion and signature help agree on which overload the user is likely trying to call.

  8. added
    Won't FixThe severity and priority of this issue do not warrant the time or complexity needed to fix it
    and removed on Oct 5, 2015
  9. RyanCavanaugh commented on Oct 5, 2015

    @RyanCavanaugh
    Member

    We'd rather error on the side of not restricting the typing here.

  10. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    SuggestionAn idea for TypeScriptWon't FixThe severity and priority of this issue do not warrant the time or complexity needed to fix it

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions