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

Discriminated parameters #24159

Closed
4 tasks done
KSXGitHub opened this issue May 16, 2018 · 5 comments
Closed
4 tasks done

Discriminated parameters #24159

KSXGitHub opened this issue May 16, 2018 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@KSXGitHub
Copy link
Contributor

KSXGitHub commented May 16, 2018

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)
@MartinJohns
Copy link
Contributor

Isn't this already solved with function overloading?

@KSXGitHub
Copy link
Contributor Author

@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
Copy link
Contributor Author

KSXGitHub commented May 16, 2018

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

@mhegazy
Copy link
Contributor

mhegazy commented May 16, 2018

Looks like a duplicate/similar use case of #24085

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 16, 2018
@typescript-bot
Copy link
Collaborator

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

@microsoft microsoft locked and limited conversation to collaborators Aug 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants