Skip to content

Partial<T, Keys> make only some properties (Keys) optional #53452

Closed as not planned
@thernstig

Description

@thernstig

Suggestion

Extend https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype from Partial<T> to Partial<T, Keys>. If Keys is not specified, it defaults to all keys.

🔍 Search Terms

partial

I already found an old, automatically closed issue, here #25760. But I feel like it is warranted to create a new issue as this should be possible to implement without a backwards-incompatible change.

✅ 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

See above.

📃 Motivating Example

interface Todo {
  title: string;
  description: string;
}
 
function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo, 'title'>) {
  return { ...todo, ...fieldsToUpdate };
}
 
const todo1 = {
  title: "organize desk",
  description: "clear clutter",
};
 
const todo2 = updateTodo(todo1, {
  description: "throw out trash",
});

// fails: 'description' is required to be updated
const todo3 = updateTodo(todo1, {
  title: "groceries",
});

💻 Use Cases

I often have types where I want all properties to be non-optional, but e.g. when reading them for the first time in an API call, or reading a file, some are optional and then I immediately set them to something if they were not included.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions