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

Improve function argument type inference #11440

Closed
Strate opened this issue Oct 7, 2016 · 4 comments
Closed

Improve function argument type inference #11440

Strate opened this issue Oct 7, 2016 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@Strate
Copy link

Strate commented Oct 7, 2016

Consider the example:

// module1.ts
export function doOperation(
  options: string,
  callback: (argument: {
    prop1: number,
    prop2: string,
    prop3: SomeComplexInterface
  }) => any
) {
  // implementation is omitted
}

// module 2
import {doOperation} from "./module1"
doOperation("options", operationCallback);
function operationCallback(argument) {
  // argument has implicit type 'any' here
}

I suggest to improve type inference of operationCallback's arguments in this way:

  1. If function don't has defined arguments
  2. Take first usage of that function
  3. Infer argument types from that usage

What do you think about it?

@aluanhaddad
Copy link
Contributor

Personally, I think this would be confusing.
I understand the desire, as I sometimes find it annoying that I have to add extra annotations when refactoring, but without those annotations the extracted callback function becomes less readable. And inferring on first use makes reuse difficult, for example if the extracted function were a generic error handler that ignored its argument.

I would suggest the following:

// module1.ts
export function doOperation(
  options: string,
  callback: (argument: OperationCallbackArgument) => any
) { ... }

export interface OperationCallbackArgument {
    prop1: number,
    prop2: string,
    prop3: SomeComplexInterface
}

// module 2
import {doOperation, OperationCallbackArgument} from "./module1"

doOperation("options", operationCallback);

function operationCallback(argument: OperationCallbackArgument) {
    console.log(argument.prop1);
}

Long term, I think the language service will provide refactorings, such as "introduce local function".

@HerringtonDarkholme
Copy link
Contributor

HerringtonDarkholme commented Oct 7, 2016

Almost impossible in TypeScript, given current architecture.
Currently the compiler only infer local expression, the "bottom up" path, or contextually typing an expression, the "top down" path. Every expression is typed "locally".

What you ask for is global type inference: feed type information back to define site from use site. That's what TypeScript is designed to do. Try flowtype for similar feature (but that's not global inference either, flow just infers the argument type from function body)

@mhegazy
Copy link
Contributor

mhegazy commented May 17, 2017

duplicate of #15114?

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 17, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Jun 2, 2017

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

@mhegazy mhegazy closed this as completed Jun 2, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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