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

Conditional types - Extract works incorrect #22899

Closed
sirian opened this issue Mar 27, 2018 · 8 comments
Closed

Conditional types - Extract works incorrect #22899

sirian opened this issue Mar 27, 2018 · 8 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@sirian
Copy link
Contributor

sirian commented Mar 27, 2018

TypeScript Version: 2.8.0-rc, 2.9.0-dev.20180327

declare function isFunction<T>(value: T): value is Extract<T, Function>;

declare function toString1(value: object | Function): string ;
declare function toString2(value: Function): string ;

function foo<T>(value: T) {
    if (isFunction(value)) {
        toString1(value); // ok

        toString2(value); // error TS2345: Argument of type 'Extract<T, Function>' is not assignable to parameter of type 'Function'.
                          // Type 'T' is not assignable to type 'Function'.
    }
}
@sirian sirian changed the title Conditional types - Extract work incorrect Conditional types - Extract works incorrect Mar 27, 2018
@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Mar 27, 2018
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 2.9 milestone Mar 27, 2018
@jcalz
Copy link
Contributor

jcalz commented Mar 27, 2018

Is this not a duplicate of #22347?

@ahejlsberg
Copy link
Member

Yes, it is pretty much a duplicate. And a workaround similar to the one mentioned in #22347 (comment) can be used:

declare function isFunction<T>(value: T): value is (Extract<T, Function> & Function);

That said, it would be nice if it actually worked, so I'm going to keep this one open as a bug.

@sirian
Copy link
Contributor Author

sirian commented Mar 27, 2018

It's wrong

declare function isObject<T>(value: T): value is (Extract<T, object> & object);

declare const foo: {key: any} | Record<string, any>;

if (isObject(foo)) {
    console.log(foo.key);
}
error TS2339: Property 'key' does not exist on type '{ key: any; } | Record<string, any>'.

@sirian
Copy link
Contributor Author

sirian commented Mar 27, 2018

moreover:

image

@ahejlsberg
Copy link
Member

@sirian That's an unrelated issue. See #21141. In your example the isObject type guard doesn't actually do anything because both sides of the union are of type object.

@ahejlsberg
Copy link
Member

See #21732 regarding the example involving the in operator.

@sirian
Copy link
Contributor Author

sirian commented Mar 28, 2018

@ahejlsberg

type Bit = 0 | 1;

declare function isBit<T>(value: T): value is Extract<T, Bit> & Bit;

declare function obj(value: object): string ;
declare function bit(value: Bit): string ;

function test1<T>(value: T) {
    if (isBit(value)) {
        obj(value);
        bit(value);
    }
}

function test2<T extends Bit>(value: T) {
    if (isBit(value)) {
        obj(value);
        bit(value);
    }
}

function test3<T extends string>(value: T) {
    if (isBit(value)) {
        obj(value);
        bit(value);
    }
}

image

@sirian
Copy link
Contributor Author

sirian commented Mar 28, 2018

@ahejlsberg maybe it should be Extract<T & Bit, Bit>?

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Mar 30, 2018
@mhegazy mhegazy modified the milestones: TypeScript 2.9, TypeScript 2.8.2 Apr 5, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants