microsoft / microsoft/TypeScript

Incorrect Intellisense for union types of types/interfaces having literals as thier prop types

Offen
#43,068 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: LS: Completion Lists Experience Enhancement Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

TS Template added by @mjbvz

TypeScript Version: 4.3.0-dev.20210303

Search Terms

  • string literal
  • suggest / suggestions / completions

  • VS Code Version:
    Version: 1.53.2
    Commit: 622cb03f7e070a9670c94bae1a45d78d7181fbd4
    Date: 2021-02-11T11:48:44.518Z
    Electron: 11.2.1
    Chrome: 87.0.4280.141
    Node.js: 12.18.3
    V8: 8.7.220.31-electron.0
    OS: Linux x64 5.4.0-66-generic

  • OS Version: Linux Mint Tricia 19.3

Issue Description
When having a union type made of interface/ type having a similar properties names and its types being union of string literals as its allowed values, the type intellisense for an variable having this union type is suggesting all sets of string literals of all types which made this union type even after one of the property is already set.

expected behaviour
when one of the property value (literal) is set in the variable having this union type, only those values of the string literals matching the underlying type whose property value (literal) is same as the one selected in the variable.

-- screen shot --
image

Extension to the issue
when using it as type for union arguments and return type in a function
returning a variable with similar structure of prop names and literal values is accepted as type of this union type, but assigning values to the property names of this variable throws error ( actually error shown at the end of this issue ). Type casting removes this error though.

Steps to Reproduce:

  1. copy and use code snippet below
type type_One_content_literals = 'A' | 'B' | 'C'
type type_Two_content_literals = 'D' | 'E' | 'F'

export type type_One = { name: 'type_One', content: type_One_content_literals }
export type type_Two = { name: 'type_Two', content: type_Two_content_literals }

export type type_union = type_One | type_Two

const func = (input: type_union): type_union => {
    // return { name: 'type_One', content: 'A' } // auto accepted as type_union but not the below case
    return { name: input.name, content: input.content } 
    // return { name: input.name, content: input.content } as type_union // type casting -removes the error
}

let tempvar: type_union = {
    name: 'type_Two',
    content: '',    // press Ctrl + Space to get intellisense
}                                                              // <-- must not show type_One_content_literals
                                                               //     when 'name' is already selected as 'type_Two'   
                                                               //     and vice versa

Errors for reference:

error thrown at function 'func'

(parameter) input: type_union
Type '{ name: "type_One" | "type_Two"; content: "A" | "B" | "C" | "D" | "E" | "F"; }' is not assignable to type 'type_union'.
  Type '{ name: "type_One" | "type_Two"; content: "A" | "B" | "C" | "D" | "E" | "F"; }' is not assignable to type 'type_Two'.
    Types of property 'name' are incompatible.
      Type '"type_One" | "type_Two"' is not assignable to type '"type_Two"'.
        Type '"type_One"' is not assignable to type '"type_Two"'.ts(2322)

error when assigning value 'A' to content of tempvar ( shows the check works after assigning value but not in intellisense or at function input arguments )

let tempvar: type_union
'tempvar' is declared but its value is never read.ts(6133)
Type '{ name: "type_Two"; content: "A"; }' is not assignable to type 'type_union'.
  Type '{ name: "type_Two"; content: "A"; }' is not assignable to type 'type_Two'.
    Types of property 'content' are incompatible.
      Type '"A"' is not assignable to type 'type_Two_content_literals'.ts(2322)

Does this issue occur when all extensions are disabled?: Yes/No - Yes it still occurs

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit der bereitgestellten TypeScript-Reproduktion und überprüfen Sie das Ctrl+Space-Vervollständigungsverhalten für die discriminated union sowie die Diagnose in func. Verfolgen Sie die beteiligten language-service-, completion- und type-checking-Einstiegspunkte; als erledigt gilt die Aufgabe, wenn die Vorschläge nach der Auswahl von name eingegrenzt werden und der Funktionsfall ohne einen cast konsistent behandelt wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.