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

Complement operator #20189

Closed
remojansen opened this issue Nov 21, 2017 · 2 comments
Closed

Complement operator #20189

remojansen opened this issue Nov 21, 2017 · 2 comments

Comments

@remojansen
Copy link
Contributor

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!

@remojansen
Copy link
Contributor Author

remojansen commented Nov 21, 2017

Duplicate of #4183 ?

@kitsonk
Copy link
Contributor

kitsonk commented Nov 21, 2017

Duplicate of #15012

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants