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

Some signatures should use union instead of overloads #5766

Description

I think, this code should be correct code by fixes of lib.d.ts or type system.

var separator: string|RegExp;
'str'.split(separator); // type error

Manual: Replace to union types

Resolve by type definition.

from:

// lib.d.ts
    split(separator: string, limit?: number): string[];
    split(separator: RegExp, limit?: number): string[];

to:

// lib.d.ts
    split(separator: string|RegExp, limit?: number): string[];

Automatic: Generate union types

Resolve by auto generated merged signature from overloads.

from:

// lib.d.ts
    split(separator: string, limit?: number): string[];
    split(separator: RegExp, limit?: number): string[];

Generate the merged signature internally, implicitly.

to:

// in memory
    split(separator: string, limit?: number): string[];
    split(separator: RegExp, limit?: number): string[];
    split(separator: string|RegExp, limit?: number): string[]; // generated

Activity

  1. ahejlsberg commented on Nov 24, 2015

    @ahejlsberg
    Member

    Agreed, we should fix the declaration of split to take a union type for the first parameter.

  2. falsandtru commented on Nov 24, 2015

    @falsandtru
    ContributorAuthor

    Thanks! Should I create a list of all integration targets? Such as String#match/replace/search, and more many types. This is steady manual works.

  3. RyanCavanaugh commented on Nov 24, 2015

    @RyanCavanaugh
    Member

    It's probably worth having a script or something to find these cases.

    Unfortunately we can't perform this merging on the type system side because the two forms (two overloads vs one overload with a union) actually have rather different semantics in practice.

  4. falsandtru commented on Nov 25, 2015

    @falsandtru
    ContributorAuthor

    All right, I'll leave it up to you.

  5. falsandtru commented on Jan 15, 2016

    @falsandtru
    ContributorAuthor

    When TypeScript fixes this issue? I want to fix this issue myself partially if TypeScript doesn't fix this issue until next version 1.8.

  6. mhegazy commented on Jan 16, 2016

    @mhegazy
    Contributor

    falsandtru (@falsandtru) feel free to send a PR for the issue.

  7. falsandtru commented on Jan 16, 2016

    @falsandtru
    ContributorAuthor

    Mohamed Hegazy (@mhegazy) thanks, I'll fix it.

  8. modified the milestone: on Sep 21, 2016
  9. changed the title [-]Integrate overloads into union types[/-] [+]`String.split` signature should use union instead of overloads[/+] on Dec 14, 2016
  10. modified the milestones: TypeScript 2.3, on Mar 13, 2017
  11. locked and limited conversation to collaborators on Jun 19, 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

    Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issueHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions