Skip to content

Partial<T> make only some properties optional #25760

Closed
@ghost

Description

Search Terms

Suggestion

Use Cases

current Partial<T> will make all properties of T optional
I want to specify only some properties of T optional

Examples

interface Message {
    mid: number;
    content: string;
}

interface MessageOptional {
   mid?: number;
   content: string;
}

const message: Message = assembleMessage({ content: 'hello' })

function assembleMessage(message: MessageOptional) : Message{
    const mid =  ... // generate mid somehow
    message.mid = mid
    return message
} 

it's very often sometimes we want to omit some properties of an object and later supplement it.
currently typescript has concepts like IntersectionUnion, how about Complement?

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. new expression-level syntax)

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions