microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.13k stars 12.5k forks source link

Autocompletion broken for generic function parameter with optional properties #30507

Closed timsuchanek closed 5 years ago

timsuchanek commented 5 years ago

As #30212 has been closed incorrectly (it's not a duplicate) and the comments there are not reacted on anymore, I reopen the issue here.

Steps to Reproduce:

typescript autocompletion

Open a TypeScript file with this content:

type Params = {
  foo?: string
  bar?: number
  visible?: boolean
}

export const obj = {
  paramsIdentityInline<T extends Params>(params: T): T {
    return params
  },
}

obj.paramsIdentityInline({

})

function direct(params: Params) {}

direct({

})

When you have a type which only consists of optional properties and use it in a generic context as a type parameter, the autocompletion doesn't work, while it works when you use that type directly without being generic in the function signature. However, as soon as at least one of the properties on that type is non-optional, the autocompletion works, even for the optional properties.

Does this issue occur when all extensions are disabled?: Yes

As @hediet correctly notes, the referenced fix doesn't work for this bug. Please see #30212 for more infos.

Ping @mjbvz

RyanCavanaugh commented 5 years ago

The problem here is that {} is a valid Params, so we think this is just a call to obj.paramsIdentityInline<{}> and the correct suggestion list is therefore empty. From a type system perspective, that's justifiable.

This doesn't happen when there's a required property because the inference fails to produce a successful call, so we defer to the constraint for error reporting purposes and the constraint does have the properties. Notice that once the required property is present, we go back to showing the completions from the inferred type: image

The "fix" is probably just a bunch more special-casing in the services layer.

schickling commented 5 years ago

Is this issue likely to be picked up by the TypeScript core team @RyanCavanaugh? :)

RyanCavanaugh commented 5 years ago

Not soon, no.

schickling commented 5 years ago

Does TypeScript have a bounty program in case someone wants to pick it up? Any thoughts on how much effort a fix for this would be?

leonardodino commented 5 years ago

This kind of issue also degrades a a bit the development experience for me.

created a reproduction at TypeScript Playground and added another case. The function being a property on an object doesn't change the auto-complete outcome at all.


Assuming I'm to fix this issue, is there any pointers where this kind of functionality is to be implemented?

Considering it provides autocomplete on error, even on the generic ones, there should be a way to reuse that code path, or am I missing something here?

timsuchanek commented 5 years ago

Opened a PR to fix this https://github.com/microsoft/TypeScript/pull/32100