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

Add RequiredProperties utility type #45768

Closed
5 tasks done
Thisen opened this issue Sep 7, 2021 · 1 comment
Closed
5 tasks done

Add RequiredProperties utility type #45768

Thisen opened this issue Sep 7, 2021 · 1 comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@Thisen
Copy link

Thisen commented Sep 7, 2021

Suggestion

πŸ” Search Terms

  • RequiredProperty
  • T & Required<Pick<T, K>>
  • RequiredProperties

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

I suggest to add a new utlity type, that makes selected properties required:

type RequiredProperties<T, K extends keyof T> = T & Required<Pick<T, K>>

πŸ“ƒ Motivating Example

This could help in such a sneario:

interface Person {
  id: string;
  name?: string;
  alias?: string;
}
// A single property
type PersonWithRequiredName = RequiredProperties<Person, "name">
const john: PersonWithRequiredName = { id: "123123", name: "john" }
// Will error, name is missing
const alice: PersonWithRequiredName = { id: "234234" }

// Multiple properties
type PersonWithRequiredNameAndAlias = RequiredProperties<Person, "name" | "alias">
const fullJohn: PersonWithRequiredNameAndAlias = { id: "123123", name: "john", alias: "j" }
// Will error, alias is missing
const fullAlice: PersonWithRequiredNameAndAlias = { id: "234234", name: "alice" }

πŸ’» Use Cases

An ideal use case would be as part of type predicates to use with Array.prototype.filter:

interface Person {
  id: string;
  name?: string;
  alias?: string;
}
type PersonWithRequiredName = RequiredProperties<Person, "name">

function hasName(person: Person): person is PersonWithRequiredName {
 return (person as PersonWithRequiredName).name !== undefined;
}
const persons: Person[] = [ ... ]
persons.filter(hasName).sort()
@MartinJohns
Copy link
Contributor

See #39522 (comment):

We've opted to not include utility type aliases in the lib unless they're required for declaration emit purposes.

@andrewbranch andrewbranch added Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript labels Sep 7, 2021
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

3 participants