microsoft / TypeScript

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

Missing IntelliSense for optional property from array type constraints #43917

Open zheeeng opened 3 years ago

zheeeng commented 3 years ago

Bug Report

🔎 Search Terms

🕗 Version & Regression Information

searched a related issue: https://github.com/microsoft/TypeScript/issues/30058

⏯ Playground Link

Playground link with relevant code

💻 Code

type ZZZ = {
    bar?: number,
    foo: string
}

function test<T extends ZZZ>(c: T) {

}

function testArr<T extends ZZZ[]>(c: T) {

}

// case 1:
test({
    // foo got intellisense
    // bar got intellisense
})

// case 2:
test({
    foo: '333',
    // bar got intellisense
})

// case 3:
testArr([
    {
        foo: '111',
        // bar got no intellisense
    },
])

// case 4:
testArr([
    {
        // foo got intellisense
        // bar got intellisense
    }
]);

🙁 Actual behavior

case 3 missing the IntelliSense for option property bar

🙂 Expected behavior

show completion for property bar

timuric commented 3 years ago

I would like to demonstrate how autocomplete is influenced by several items in array:

type ZZZ = {
    foo: number;
    bar?: number;
    baz?: number;
}

function testArr<T extends ZZZ[]>(c: T) {

}

// case 1:
testArr([
    {
     foo: 1,
     baz: 1
    },
    {
      foo: 1,
      //baz has autcomplete
      //bar missing autocomplete
    }
]);

// case 2:
testArr([
    {
     foo: 1,
     //Neither baz or bar have autcomplete
    },
    {
      foo: 1,
      //Neither baz or bar have autcomplete
    }
]);

// case 3:
testArr([
    {
     foo: 1,
     //bar and baz appear in autcomplete 
    },
    {
      foo: 1,
      bar: 1,
       //bar and baz appear in autcomplete 
    },
    {
        //bar, baz and foo appear in autocomplete
    }
]);

playground link