镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

inference problem with constraints involving conditionals and generics  #29662

Description

the following is seen in typescript@3.4.0-dev.20190131:

declare function onlyNullablePlease<T extends (null extends T ? any : never)>(value: T): void;

declare var z: string | null;
onlyNullablePlease(z); // works as expected

declare var y: string;
onlyNullablePlease(y); // error as expected

function f<T>(t: T) {
    var x: T | null = Math.random() > 0.5 ? null : t;
    onlyNullablePlease(x); // should work, unexpected error:
    /*
Argument of type 'T | null' is not assignable to parameter of type 'null extends T | null ? any : never'.
  Type 'null' is not assignable to type 'null extends T | null ? any : never'.ts(2345)
    */
}

Activity

  1. jack-williams commented on Jan 31, 2019

    @jack-williams
    Collaborator

    I suspect this PR #29338 introduced the delta, not the one you link. I also think this is related to #29505. Wrapping the check and extends types in a 1-tuple is a short-term fix.

    declare function onlyNullablePlease<T extends ([null] extends [T] ? unknown : never)>(value: T): void;
  2. zpdDG4gta8XKpMCd commented on Feb 27, 2019

    @zpdDG4gta8XKpMCd
    Author

    fyi, the problem is a major breaking change in our project, it prevents us from upgrading to the latest version of typescript, the workaround by Jack Williams (@jack-williams) helped though

  3. weswigham commented on Feb 27, 2019

    @weswigham
    Member

    Personal request: Use unknown instead of any for something like the example in the OP - type parameters extending any are currently.... strange (they're kinda checked and kinda not checked). See #29571 where we plan to fix the strangeness.

  4. zpdDG4gta8XKpMCd commented on Mar 1, 2019

    @zpdDG4gta8XKpMCd
    Author

    unknown doesn't help

  5. weswigham commented on Mar 1, 2019

    @weswigham
    Member

    Oh, yeah, it doesn't help here, but I just wanted to inform that extends any behaves poorly and might want to be avoided, since extends unknown behaves far better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions