diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 014443b6ea4..dde8033d1e9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5794,6 +5794,10 @@ namespace ts { if (type.flags & TypeFlags.Union) { const props = createMap(); for (const memberType of (type as UnionType).types) { + if (memberType.flags & TypeFlags.Primitive) { + continue; + } + for (const { name } of getPropertiesOfType(memberType)) { if (!props.has(name)) { props.set(name, createUnionOrIntersectionProperty(type as UnionType, name)); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 567cfa7f773..a623da1f461 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2587,6 +2587,7 @@ namespace ts { /** * For a union, will include a property if it's defined in *any* of the member types. * So for `{ a } | { b }`, this will include both `a` and `b`. + * Does not include properties of primitive types. */ /* @internal */ getAllPossiblePropertiesOfType(type: Type): Symbol[]; } diff --git a/tests/cases/fourslash/completionListOfUnion.ts b/tests/cases/fourslash/completionListOfUnion.ts index 6026615f8c1..5ffaf89e5b3 100644 --- a/tests/cases/fourslash/completionListOfUnion.ts +++ b/tests/cases/fourslash/completionListOfUnion.ts @@ -2,17 +2,19 @@ // @strictNullChecks: true -// Non-objects should be skipped, so `| number | null` should have no effect on completions. -////const x: { a: number, b: number } | { a: string, c: string } | { b: boolean } | number | null = { /*x*/ }; +// Primitives should be skipped, so `| number | null | undefined` should have no effect on completions. +////const x: { a: number, b: number } | { a: string, c: string } | { b: boolean } | number | null | undefined = { /*x*/ }; ////interface I { a: number; } ////function f(...args: Array) {} ////f({ /*f*/ }); goTo.marker("x"); +verify.completionListCount(3); verify.completionListContains("a", "(property) a: string | number"); verify.completionListContains("b", "(property) b: number | boolean"); verify.completionListContains("c", "(property) c: string"); goTo.marker("f"); verify.completionListContains("a", "(property) a: number"); +// Also contains array members