TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Autocomplete on extends keyof generic

Open lsagetlethias opened this issue 6 years ago • 4 comments

TypeScript Version: 3.3.0-dev.20181122

Search Terms:

  • autocomplete
  • autocomplete generic
  • generic extends
  • autocomplete keyof extends
  • autocomplete keyof generic

Code

type Except<T, K extends keyof T> = Pick<T, { [P in keyof T]: P extends K ? never : P }[keyof T]>;

interface I1 {
    foo: string;
}
function F1<T extends I1>(): Except<T, ''> { // error is shown but autocomplete is not provided
    //code
    return null;
}

Expected behavior: Having autocomplete showing when extending a generic

Actual behavior: Even if error is correctly displayed, no autocomplete is shown for a T extends with a known type on the right hand.

Playground Link: https://www.typescriptlang.org/play/index.html#src=type%20Except%3CT%2C%20K%20extends%20keyof%20T%3E%20%3D%20Pick%3CT%2C%20%7B%20%5BP%20in%20keyof%20T%5D%3A%20P%20extends%20K%20%3F%20never%20%3A%20P%20%7D%5Bkeyof%20T%5D%3E%3B%0A%0Ainterface%20I1%20%7B%0A%20%20%20%20foo%3A%20string%3B%0A%7D%0Afunction%20F1%3CT%20extends%20I1%3E()%3A%20Except%3CT%2C%20''%3E%20%7B%0A%20%20%20%20%2F%2Fcode%0A%20%20%20%20return%20null%3B%0A%7D

Related Issues: #16740 - but this one is quite different because it's a keyof issue

lsagetlethias avatar Nov 24 '18 15:11 lsagetlethias

I got very confused when extending the generic. Check this out for some sort of explanation why this probably isn't implemented: https://stackoverflow.com/a/58417200/7731090. I do one up your suggestion nonetheless.

I can understand why it wouldn't type, because the generic is generic . The other side of the coin is that it should at least provide values on the values it DOES know. I think it would stop some confusion and nice to have, but what drawbacks are there to consider?

waynevanson avatar Oct 16 '19 21:10 waynevanson

I don't think about any major drawbacks; I think there will even be some good side-effects.

Going from this: image

To this: image will also limit noises in Intellisense results.

lsagetlethias avatar Oct 18 '19 12:10 lsagetlethias

I found another example (more simple).

playground

interface I1 {
    foo: number;
}

// Working
type Working = Pick<I1, 'foo'>;

// Not working
interface NotWorking extends Pick<I1, 'foo'> {}

image image

chrg1001 avatar May 16 '22 08:05 chrg1001

This still persists at typescript version 4.8.4

eythaann avatar Oct 17 '22 20:10 eythaann

Here's another example that seems to be caused by the same root issue: Typescript Playground

ciriousjoker avatar Mar 17 '23 17:03 ciriousjoker

FWIW keyof is not necessary in the generic function case. This example also does not provide type-aware autocomplete suggestions:

declare const example: <T extends 'foo'>() => unknown
example<''>()
//       ^

mkantor avatar May 08 '23 15:05 mkantor