Skip to content

Conditional types wrongly work with indexed access and object type (regression) #23800

Closed

Description

@falsandtru

cc Anders Hejlsberg (@ahejlsberg)

TypeScript Version: 2.9.0-dev.20180501

Search Terms:

Code

type A<T, V> = { [P in keyof T]: T[P] extends V ? 1 : 0; };
type B<T, V> = { [P in keyof T]: T[P] extends V | object ? 1 : 0; };

type a = A<{ a: 0 | 1 }, 0>; // { a: 0; }
type b = B<{ a: 0 | 1 }, 0>; // { a: 1; }

Expected behavior:

type b = B<{ a: 0 | 1 }, 0>; // { a: 0; }

Actual behavior:

type b = B<{ a: 0 | 1 }, 0>; // { a: 1; }

Playground Link:

Related Issues:

Activity

  1. mhegazy commented on May 1, 2018

    @mhegazy
    Contributor

    I see both as { a: 0 } on latest and on 2.8.3 (playground)

  2. mhegazy commented on May 1, 2018

    @mhegazy
    Contributor

    never mind.. looks like we broke it yesterday.

  3. ahejlsberg commented on May 1, 2018

    @ahejlsberg
    Member

    The immediate cause of this is #23768. However, it just exposes indexed access types to an issue that already exists with regular type parameters:

    function foo<T, U>(x: T) {
        let a: object = x;  // Error as expected
        let b: U | object = x;  // No error!
    }

    The problem is caused by some logic in structuredTypeRelatedTo that specifically excludes checking the default {} constraint of an unconstrained type parameter against a target type of object. However, that logic doesn't kick in when the target is a union type that includes object.

    Instead of attempting to exclude cases, when the source is an unconstrained type variable I think we need to check if the target is an empty object type or a union containing an empty object type.

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

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions