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

Infer extra parameter types from default values #17559

Closed
falsandtru opened this issue Aug 2, 2017 · 9 comments
Closed

Infer extra parameter types from default values #17559

falsandtru opened this issue Aug 2, 2017 · 9 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@falsandtru
Copy link
Contributor

TypeScript Version: nightly (2.5.0-dev.20170801)

Code

const cb: () => void = (a = 0) => a;
((cb: () => void) => cb)((a = 0) => a);

Expected behavior:

a is number type.

Actual behavior:

a is any type.

@DanielRosenwasser
Copy link
Member

Thanks for linking to that @olegdunkan! It's indeed the specified behavior, though it's certainly surprising and it looks like noImplicitAny doesn't catch it.

@DanielRosenwasser DanielRosenwasser added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Aug 2, 2017
@falsandtru
Copy link
Contributor Author

This spec seems to say about default behaviors that has no default value. @ahejlsberg Does this spec mean that type inference has to ignore a type of default value too and still should be so even today?

@falsandtru falsandtru changed the title Infer extra parameter types with initializing Infer extra parameter types from default values Aug 2, 2017
@simonbuchan
Copy link

Seems like a spec bug: considered to have type Any should probably be whatever spec language would be for considered to have the type that would be inferred without a contextual signature.

@RyanCavanaugh RyanCavanaugh added Needs More Info The issue still hasn't been fully clarified and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Oct 10, 2017
@RyanCavanaugh
Copy link
Member

@falsandtru how did you even end up in this state?

@falsandtru
Copy link
Contributor Author

falsandtru commented Oct 10, 2017

To define and initialize local variables. For example,

const count = ((a = 0) => () => ++a)();

can be used as shorthand for

const count = (() => {
    let a = 0;
    return () => ++a;
})();

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript and removed Needs More Info The issue still hasn't been fully clarified labels Oct 10, 2017
@falsandtru
Copy link
Contributor Author

Sorry, my previous example is wrong. Here is a correct one.

f((a = 0) => ++a);

function f(cb: () => void) {
}

@RyanCavanaugh
Copy link
Member

Example in OP now works as hoped

@simonbuchan
Copy link

Aww, I was hoping that that would mean this works:

function getOr<T, K extends keyof T, D>(
  obj: T,
  key: K,
  // Type 'undefined' is not assignable to type 'D'.
  //  'D' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.(2322)
  defaultValue: D = undefined,
): T[K] | D {
    if (key in obj) {
        return obj[key];
    } else {
        return defaultValue;
    }
}

// $ExpectType string | undefined
getOr(process.env, "SOME_VAR");
// $ExpectType string | null
getOr(process.env, "SOME_VAR", null);
// $ExpectType string
getOr(process.env, "SOME_VAR", "DEFAULT");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants