Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New RequireProp utility type #38854

Closed
5 tasks done
elizabeth-dev opened this issue May 29, 2020 · 1 comment
Closed
5 tasks done

New RequireProp utility type #38854

elizabeth-dev opened this issue May 29, 2020 · 1 comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@elizabeth-dev
Copy link

elizabeth-dev commented May 29, 2020

Search Terms

require, require key, RequireProp, utility require, individual Required

Suggestion

Add a new RequiredProp utility type, which constructs a new type based on a passed interface, but requiring an specified property. It would be opposed to Omit utility type, and would use an approach similar to Required, but looking for an individual property. The implementation would be something like this:

type RequireProp<T, K extends keyof T> = T & { [P in K]-?: T[P] };

An alternative name could be RequireKey.

I was already preparing a PR, but as stated in the contributing guidelines, it is required for suggestions to be approved by a project maintainer first.

Use Cases

This would be useful in cases where the user can be confident about a property being set on an object. Functions that take a value and require a specific property to be set to work properly can benefit from this too.

I couldn't find a current approach for this other than self-declaring a similar utility type at project level, or creating a new interface that extends the "parent" one, requiring the necessary property.

Examples

interface Props {
    a: number;
    b?: number;
    c?: number;
}

const obj: Props = { a: 5 }; // OK
const obj2: RequireProp<Props, 'b'> = { a: 5 }; // Error: property 'b' missing
const obj3: RequireProp<Props, 'b'> = { a: 5, b: 10 }; // OK

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code // If a type named "RequireProp" is already declared in the project, tsc will prioritize the user-defined type over the library one
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript labels Jun 11, 2020
@RyanCavanaugh
Copy link
Member

We've opted to not include utility type aliases in the lib unless they're required for declaration emit purposes, since people tend to not be happy whenever we pick a definition that isn't exactly the one they're already using

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants