Skip to content

Discriminated parameters #24159

Closed
Closed
@KSXGitHub

Description

@KSXGitHub

Search Terms

Suggestion

In Node.js, there's some methods like fs.readFile that passes 2 parameters to callback function: (err: Error | null, data: Buffer | null) => void. If err is null then data is not null and vice versa. It would be extremely useful if we have some sort of discriminated parameters.

Use Cases

Examples

As a callback

// Callback function type which takes two sets of parameters
interface Callback<X, E = Error> { 
    (error: E): void
    (error: null, data: X): void
}

interface Buffer {}

declare function readFile(
    filename: string,
    callback: Callback<Buffer>
): void

// What I want to be able to do
readFile(
    'file.txt',
    (error, data) => { // TypeScript figures out parameters' type
        if (error === null) {
            const err: null = error
        } else {
            const err: Error = error
            const buf: Buffer = data
        }
    }
)

As a function expression

interface Fn<A, B> {
  (type: 'a', value: A): void
  (type: 'b', value: B): void
}

const fn: Fn<string, number> = (type, value) => {
  const T: 'a' | 'b' = type
  const X: string | number = value
  
  // TypeScript deduces types of `foo` and `bar` in each branch
  if (type === 'a') {
    const foo: 'a' = type
    const bar: string = value
  } else {
    const foo: 'b' = type
    const bar: number = value
  }
}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
    This wouldn't change the runtime behavior of existing JavaScript code
    This could be implemented without emitting different JS based on the types of the expressions
    This isn't a runtime feature (e.g. new expression-level syntax)

Activity

MartinJohns

MartinJohns commented on May 16, 2018

@MartinJohns
Contributor

Isn't this already solved with function overloading?

KSXGitHub

KSXGitHub commented on May 16, 2018

@KSXGitHub
ContributorAuthor

@MartinJohns Please focus on // What I want to be able to do. The point is, I want TypeScript to be able to figure out type of data based on type/value of error.

KSXGitHub

KSXGitHub commented on May 16, 2018

@KSXGitHub
ContributorAuthor

Speaking of function overloading, TypeScript cannot deduce type of one parameter base on another parameter.

mhegazy

mhegazy commented on May 16, 2018

@mhegazy
Contributor

Looks like a duplicate/similar use case of #24085

typescript-bot

typescript-bot commented on Jun 2, 2018

@typescript-bot
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

locked and limited conversation to collaborators on Aug 2, 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

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @MartinJohns@mhegazy@KSXGitHub@typescript-bot

        Issue actions

          Discriminated parameters · Issue #24159 · microsoft/TypeScript