Skip to content

Stricter interfaces/literal types #6352

Closed
@ewinslow

Description

@ewinslow

I love #3823 and the way it catches lots of errors early. Unfortunately, it doesn't quite go as far as I'd like it to. Suppose you have code like this, where inlining all properties of styles.header is undesirable for whatever reason (e.g., you can imagine such a block getting very large with many style options to set):

function applyStyles(el: Element, style: {backgroundColor: string}) {}

const styles = {
  header: {
    backroundColor: '#eee',
  },
};

applyStyles(document.getElementById('#header'), styles.header);

Here, the applyStyles call will not catch the extra "typo" property (backroundColor instead of backgroundColor) in the shape of styles.header, which has all optional properties (corresponding to Element.prototype.style properties).

My only option to make sure these things checked right now is I guess to maintain a whitelist of accepted properties that is verified at runtime for each call to the function in question. A way to check this at compile time instead seems like it would be pretty valuable. I know this was discussed before and turned down in favor of only checking inline object literals, but I didn't see this kind of use-case clearly articulated in that discussion so I figured I'd open another issue and see if anyone else still thinks there should be a way to say "no, truly, do not ever pass me anything other than this set of options".

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions