Skip to content

Is this possible? Opposite of Partial Type #12578

Closed

Description

@opensrcken

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'.

Activity

  1. mhegazy commented on Nov 30, 2016

    @mhegazy
    Contributor

    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
  2. ahejlsberg commented on Nov 30, 2016

    @ahejlsberg
    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
  3. ahejlsberg commented on Nov 30, 2016

    @ahejlsberg
    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).

  4. opensrcken commented on Nov 30, 2016

    @opensrcken
    Author

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

  5. k8w commented on Jun 23, 2017

    @k8w

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

  6. mjomble commented on Jun 1, 2018

    @mjomble

    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

  7. locked and limited conversation to collaborators on Jul 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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