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

Is this possible? Opposite of Partial Type #12578

Closed
opensrcken opened this issue Nov 30, 2016 · 6 comments
Closed

Is this possible? Opposite of Partial Type #12578

opensrcken opened this issue Nov 30, 2016 · 6 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@opensrcken
Copy link

opensrcken commented Nov 30, 2016

TypeScript Version: nightly

Code

interface Test {
  optional?: string;
}

function getProperty<K extends keyof Test>(t: Test, key: K): Test[K]|never {
  if (t[key]) {
    return t[key];
  }
  throw new Error('Value does not exist at key');
}

const a: string = getProperty({}, 'optional');

Target behavior:
The given function is coded such that it will always return a non-empty value, so it would be great if there were a way to make something like this compile. Is there a way to communicate the opposite of Partial<T>, e.g. that we know Test[K] will be non-empty?

Actual behavior:
error TS2322: Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 30, 2016

You can use inference from homomorphic mapped types in #12528 to get a type that removes the undefined

for instance:

interface Test {
    optional?: string;
    required: number;
}

type OptionalMap<T> = {
    [P in keyof T]: T[P] | undefined;
};

function getProperty<T, K extends keyof T>(t: T, key: K): T[K] {
    return t[key];
}

function getPropertyRemoveUndefined<T, K extends keyof T>(t: OptionalMap<T>, key: K): T[K] {
    // Put it in a value, since `t[p]` is not narrowed only t.p which is not possible here
    var value = t[key];
    if (value) {
        return value;
    }
    throw new Error('Value does not exist at key');
}

var t: Test = {required: 0};


var o1 = getProperty(t, 'optional'); // string | undefined
var r1 = getProperty(t, 'required'); // number

var o2 = getPropertyRemoveUndefined(t, 'optional'); // string
var r2 = getPropertyRemoveUndefined(t, 'required'); // number

@ahejlsberg
Copy link
Member

I just merged #12589 and now you can simply use the predefined Partial<T> type:

interface Test {
    optional?: string;
    required: number;
}

function getProperty<T, K extends keyof T>(t: T, key: K): T[K] {
    return t[key];
}

function getPropertyRemoveUndefined<T, K extends keyof T>(t: Partial<T>, key: K): T[K] {
    // Put it in a value, since `t[p]` is not narrowed only t.p which is not possible here
    var value = t[key];
    if (value) {
        return value;
    }
    throw new Error('Value does not exist at key');
}

var t: Test = {required: 0};


var o1 = getProperty(t, 'optional'); // string | undefined
var r1 = getProperty(t, 'required'); // number

var o2 = getPropertyRemoveUndefined(t, 'optional'); // string
var r2 = getPropertyRemoveUndefined(t, 'required'); // number

@ahejlsberg
Copy link
Member

@opensrcken BTW, I noticed that you used a union Test[K] | never. Just wanted to point out that the never type is redundant here as it is always removed from union types (it effectively represents an empty union type).

@opensrcken
Copy link
Author

It works. And point taken on never in unions. Thanks guys. 👍

@k8w
Copy link

k8w commented Jun 23, 2017

So I must write a function to get these opposite type ?
Can I just remove undefined or null from a type just like use ! ?

@mjomble
Copy link

mjomble commented Jun 1, 2018

TypeScript 2.8 introduces Required<T> that may give you what you need: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#example-6

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

6 participants