Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/compiler/checker.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -722,6 +722,7 @@ namespace ts {
const templateLiteralTypes = new Map<string, TemplateLiteralType>();
const stringMappingTypes = new Map<string, StringMappingType>();
const substitutionTypes = new Map<string, SubstitutionType>();
const propertyWitnessRecordTypes = new Map<string, Type>();
const evolvingArrayTypes: EvolvingArrayType[] = [];
const undefinedProperties: SymbolTable = new Map();

Expand DownExpand Up@@ -915,6 +916,7 @@ namespace ts {
let deferredGlobalExtractSymbol: Symbol;
let deferredGlobalOmitSymbol: Symbol;
let deferredGlobalBigIntType: ObjectType;
let deferredGlobalRecordSymbol: Symbol;

const allPotentiallyUnusedIdentifiers = new Map<Path, PotentiallyUnusedIdentifier[]>(); // key is file name

Expand DownExpand Up@@ -12658,6 +12660,10 @@ namespace ts {
return deferredGlobalBigIntType || (deferredGlobalBigIntType = getGlobalType("BigInt" as __String, /*arity*/ 0, reportErrors)) || emptyObjectType;
}

function getGlobalRecordSymbol(): Symbol {
return deferredGlobalRecordSymbol || (deferredGlobalRecordSymbol = getGlobalSymbol("Record" as __String, SymbolFlags.TypeAlias, Diagnostics.Cannot_find_global_type_0)!);
}

/**
* Instantiates a global type that is generic with some element type, and returns that instantiation.
*/
Expand DownExpand Up@@ -22169,7 +22175,20 @@ namespace ts {
|| isThisTypeParameter(type)
|| type.flags & TypeFlags.Intersection && every((type as IntersectionType).types, t => t.symbol !== globalThisSymbol)) {
const propName = escapeLeadingUnderscores(literal.text);
return filterType(type, t => isTypePresencePossible(t, propName, assumeTrue));
const narrowed = filterType(type, t => isTypePresencePossible(t, propName, assumeTrue));
if (assumeTrue && (narrowed.flags & TypeFlags.Never)) {
const recordTypeAlias = getGlobalRecordSymbol();
if (!recordTypeAlias) {
return errorType;
}
let record = propertyWitnessRecordTypes.get(literal.text);
if (!record) {
record = getTypeAliasInstantiation(recordTypeAlias, [getLiteralType(literal.text), unknownType]);
propertyWitnessRecordTypes.set(literal.text, record);
}
return getIntersectionType([type, record]);
}
return narrowed;
}
return type;
}
Expand Down
12 changes: 8 additions & 4 deletions tests/baselines/reference/inKeywordTypeguard.errors.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,8 +5,10 @@ tests/cases/compiler/inKeywordTypeguard.ts(16,11): error TS2339: Property 'a' do
tests/cases/compiler/inKeywordTypeguard.ts(27,11): error TS2339: Property 'b' does not exist on type 'AWithOptionalProp | BWithOptionalProp'.
Property 'b' does not exist on type 'AWithOptionalProp'.
tests/cases/compiler/inKeywordTypeguard.ts(42,11): error TS2339: Property 'b' does not exist on type 'AWithMethod'.
tests/cases/compiler/inKeywordTypeguard.ts(49,11): error TS2339: Property 'a' does not exist on type 'never'.
tests/cases/compiler/inKeywordTypeguard.ts(50,11): error TS2339: Property 'b' does not exist on type 'never'.
tests/cases/compiler/inKeywordTypeguard.ts(49,11): error TS2339: Property 'a' does not exist on type '(AWithMethod & Record<"c", unknown>) | (BWithMethod & Record<"c", unknown>)'.
Property 'a' does not exist on type 'BWithMethod & Record<"c", unknown>'.
tests/cases/compiler/inKeywordTypeguard.ts(50,11): error TS2339: Property 'b' does not exist on type '(AWithMethod & Record<"c", unknown>) | (BWithMethod & Record<"c", unknown>)'.
Property 'b' does not exist on type 'AWithMethod & Record<"c", unknown>'.
tests/cases/compiler/inKeywordTypeguard.ts(52,11): error TS2339: Property 'a' does not exist on type 'AWithMethod | BWithMethod'.
Property 'a' does not exist on type 'BWithMethod'.
tests/cases/compiler/inKeywordTypeguard.ts(53,11): error TS2339: Property 'b' does not exist on type 'AWithMethod | BWithMethod'.
Expand DownExpand Up@@ -85,10 +87,12 @@ tests/cases/compiler/inKeywordTypeguard.ts(94,26): error TS2339: Property 'a' do
if ("c" in x) {
x.a();
~
!!! error TS2339: Property 'a' does not exist on type 'never'.
!!! error TS2339: Property 'a' does not exist on type '(AWithMethod & Record<"c", unknown>) | (BWithMethod & Record<"c", unknown>)'.
!!! error TS2339: Property 'a' does not exist on type 'BWithMethod & Record<"c", unknown>'.
x.b();
~
!!! error TS2339: Property 'b' does not exist on type 'never'.
!!! error TS2339: Property 'b' does not exist on type '(AWithMethod & Record<"c", unknown>) | (BWithMethod & Record<"c", unknown>)'.
!!! error TS2339: Property 'b' does not exist on type 'AWithMethod & Record<"c", unknown>'.
} else {
x.a();
~
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/inKeywordTypeguard.types
Original file line numberDiff line numberDiff line change
Expand Up@@ -146,13 +146,13 @@ function negativeTestClassesWithMemberMissingInBothClasses(x: AWithMethod | BWit
x.a();
>x.a() : any
>x.a : any
>x : never
>x : (AWithMethod & Record<"c", unknown>) | (BWithMethod & Record<"c", unknown>)
>a : any

x.b();
>x.b() : any
>x.b : any
>x : never
>x : (AWithMethod & Record<"c", unknown>) | (BWithMethod & Record<"c", unknown>)
>b : any

} else {
Expand Down