这是indexloc提供的服务,不要输入任何密码
Skip to content

Exact Object  #319

@karlhorky

Description

@karlhorky

🍩 Feature Request

Is your feature request related to a problem?

It would be great to have an exact object / interface utility type as a part of ts-toolbelt - preventing any fewer properties or excess properties passed in an object - such as:

function fn(arg: Exact<{ x: string }>) {}

// ok
fn({ x: '' });

// errors
fn({ y: '' });
fn({ x: '', y: '' });

This is one of the top-requested features in TypeScript itself, but does not yet exist in the language.

Describe the solution you'd like

A new exported Object/Exact utility type to allow for checking against exact types

Describe alternatives you've considered

I saw Function/Exact, but this appears to be something different... This docs page does use some of the same words when describing the problem, which at first seems to indicate it's the right thing:

Force A to comply with W. A must be a shape of W. In other words, A must extend W and have the same properties - no more, no less.

...but I don't understand how / where I would apply this Exact<A, W> generic signature. 🤔

Teachability, Documentation, Adoption, Migration Strategy

For implementation of the utility, some of the answers in the TypeScript issue are potentially useful:

The code block in the first section above could be used an example in the docs for the type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions