Skip to content

Complement operator #20189

Closed
Closed
@remojansen

Description

@remojansen

Hi,

We are able to map from an object into a partial object using a mapped type. For example, given:

interface User {
    name: string;
    age: number;
}

We can map it using Partial<T>:

type PartialUser = Partial<User>;

The type PartialUser is equivalent to:

interface PartialUser {
    name?: string;
    age?: number;
}

But we are not able to map from a partial object to a non-partial object? For example, given:

interface PartialUser {
    name?: string;
    age?: number;
}

If would be nice to be able to map it into the following:

interface NotPartialUser {
    name: string;
    age: number;
}

Using a mapped type:

type NotPartialUser = NotPartial<PartialUser>;

To implement NotPartial<T> we need the complement operator? or there is a way to achieve this today?

The complement operator could look as follows. I don't really care about the syntax but here are some options:

type NotPartial<T> = {
    [P in keyof T]: T[P] ~ undefined;
};
type NotPartial<T> = {
    [P in keyof T]: T[P] \ undefined;
};
type NotPartial<T> = {
    [P in keyof T]: T[P] \\ undefined;
};
type NotPartial<T> = {
    [P in keyof T]: T[P] - undefined;
};
type NotPartial<T> = {
    [P in keyof T]: T[P] -- undefined;
};

This operator exists in other programming languages but not always at the type system.

We have unions an intersections already. It seems reasonable to me to expect other set operations to be available but please let me know if I'm wrong and I'm missing something.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions