From 745f958edb5ff30625457371e855579ede3279b1 Mon Sep 17 00:00:00 2001 From: TypeScript Bot Date: Thu, 11 Aug 2022 15:33:56 -0700 Subject: [PATCH] Cherry-pick PR #50279 into release-4.8 (#50280) Component commits: eff4406f0e Revert "Fixed an issue with contextual type for intersection properties (#48668)" This reverts commit 9236e39374c0ec9a1e3f9894af4fb9eb34ba0021. Co-authored-by: Ryan Cavanaugh --- src/compiler/checker.ts | 34 +------- ...lTypeFunctionObjectPropertyIntersection.js | 42 ---------- ...FunctionObjectPropertyIntersection.symbols | 82 ------------------- ...peFunctionObjectPropertyIntersection.types | 73 ----------------- ...lTypeFunctionObjectPropertyIntersection.ts | 29 ------- 5 files changed, 4 insertions(+), 256 deletions(-) delete mode 100644 tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.js delete mode 100644 tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.symbols delete mode 100644 tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.types delete mode 100644 tests/cases/compiler/contextualTypeFunctionObjectPropertyIntersection.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2b3cdcb736d1a..d5a587415d790 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -27124,27 +27124,7 @@ namespace ts { } function getTypeOfPropertyOfContextualType(type: Type, name: __String, nameType?: Type) { - return mapType(type, (t): Type | undefined => { - if (t.flags & TypeFlags.Intersection) { - const intersection = t as IntersectionType; - let newTypes = mapDefined(intersection.types, getTypeOfConcretePropertyOfContextualType); - if (newTypes.length > 0) { - return getIntersectionType(newTypes); - } - newTypes = mapDefined(intersection.types, getTypeOfApplicableIndexInfoOfContextualType); - if (newTypes.length > 0) { - return getIntersectionType(newTypes); - } - return undefined; - } - const concretePropertyType = getTypeOfConcretePropertyOfContextualType(t); - if (concretePropertyType) { - return concretePropertyType; - } - return getTypeOfApplicableIndexInfoOfContextualType(t); - }, /*noReductions*/ true); - - function getTypeOfConcretePropertyOfContextualType(t: Type) { + return mapType(type, t => { if (isGenericMappedType(t) && !t.declaration.nameType) { const constraint = getConstraintTypeFromMappedType(t); const constraintOfConstraint = getBaseConstraintOfType(constraint) || constraint; @@ -27152,9 +27132,8 @@ namespace ts { if (isTypeAssignableTo(propertyNameType, constraintOfConstraint)) { return substituteIndexedMappedType(t, propertyNameType); } - return undefined; } - if (t.flags & TypeFlags.StructuredType) { + else if (t.flags & TypeFlags.StructuredType) { const prop = getPropertyOfType(t, name); if (prop) { return isCircularMappedProperty(prop) ? undefined : getTypeOfSymbol(prop); @@ -27165,15 +27144,10 @@ namespace ts { return restType; } } + return findApplicableIndexInfo(getIndexInfosOfStructuredType(t), nameType || getStringLiteralType(unescapeLeadingUnderscores(name)))?.type; } return undefined; - } - function getTypeOfApplicableIndexInfoOfContextualType(t: Type) { - if (!(t.flags & TypeFlags.StructuredType)) { - return undefined; - } - return findApplicableIndexInfo(getIndexInfosOfStructuredType(t), nameType || getStringLiteralType(unescapeLeadingUnderscores(name)))?.type; - } + }, /*noReductions*/ true); } // In an object literal contextually typed by a type T, the contextual type of a property assignment is the type of diff --git a/tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.js b/tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.js deleted file mode 100644 index 65213deade435..0000000000000 --- a/tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.js +++ /dev/null @@ -1,42 +0,0 @@ -//// [contextualTypeFunctionObjectPropertyIntersection.ts] -type Action = (ev: TEvent) => void; - -interface MachineConfig { - schema: { - events: TEvent; - }; - on?: { - [K in TEvent["type"]]?: Action; - } & { - "*"?: Action; - }; -} - -declare function createMachine( - config: MachineConfig -): void; - -createMachine({ - schema: { - events: {} as { type: "FOO" } | { type: "BAR" }, - }, - on: { - FOO: (ev) => { - ev.type; // should be 'FOO' - }, - }, -}); - - -//// [contextualTypeFunctionObjectPropertyIntersection.js] -"use strict"; -createMachine({ - schema: { - events: {} - }, - on: { - FOO: function (ev) { - ev.type; // should be 'FOO' - } - } -}); diff --git a/tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.symbols b/tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.symbols deleted file mode 100644 index 4690985350a17..0000000000000 --- a/tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.symbols +++ /dev/null @@ -1,82 +0,0 @@ -=== tests/cases/compiler/contextualTypeFunctionObjectPropertyIntersection.ts === -type Action = (ev: TEvent) => void; ->Action : Symbol(Action, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 0)) ->TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 12)) ->type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 28)) ->ev : Symbol(ev, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 48)) ->TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 12)) - -interface MachineConfig { ->MachineConfig : Symbol(MachineConfig, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 68)) ->TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 24)) ->type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 40)) - - schema: { ->schema : Symbol(MachineConfig.schema, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 58)) - - events: TEvent; ->events : Symbol(events, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 3, 11)) ->TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 24)) - - }; - on?: { ->on : Symbol(MachineConfig.on, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 5, 4)) - - [K in TEvent["type"]]?: Action; ->K : Symbol(K, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 7, 5)) ->TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 24)) ->Action : Symbol(Action, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 0)) ->TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 24)) ->type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 7, 51)) ->K : Symbol(K, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 7, 5)) ->TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 24)) - - } & { - "*"?: Action; ->"*" : Symbol("*", Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 8, 7)) ->Action : Symbol(Action, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 0)) ->TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 24)) - - }; -} - -declare function createMachine( ->createMachine : Symbol(createMachine, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 11, 1)) ->TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 13, 31)) ->type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 13, 47)) - - config: MachineConfig ->config : Symbol(config, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 13, 64)) ->MachineConfig : Symbol(MachineConfig, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 68)) ->TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 13, 31)) - -): void; - -createMachine({ ->createMachine : Symbol(createMachine, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 11, 1)) - - schema: { ->schema : Symbol(schema, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 17, 15)) - - events: {} as { type: "FOO" } | { type: "BAR" }, ->events : Symbol(events, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 18, 11)) ->type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 19, 19)) ->type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 19, 37)) - - }, - on: { ->on : Symbol(on, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 20, 4)) - - FOO: (ev) => { ->FOO : Symbol(FOO, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 21, 7)) ->ev : Symbol(ev, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 22, 10)) - - ev.type; // should be 'FOO' ->ev.type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 19, 19)) ->ev : Symbol(ev, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 22, 10)) ->type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 19, 19)) - - }, - }, -}); - diff --git a/tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.types b/tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.types deleted file mode 100644 index 30ba52626faa5..0000000000000 --- a/tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.types +++ /dev/null @@ -1,73 +0,0 @@ -=== tests/cases/compiler/contextualTypeFunctionObjectPropertyIntersection.ts === -type Action = (ev: TEvent) => void; ->Action : Action ->type : string ->ev : TEvent - -interface MachineConfig { ->type : string - - schema: { ->schema : { events: TEvent; } - - events: TEvent; ->events : TEvent - - }; - on?: { ->on : ({ [K in TEvent["type"]]?: Action | undefined; } & { "*"?: Action | undefined; }) | undefined - - [K in TEvent["type"]]?: Action; ->type : K - - } & { - "*"?: Action; ->"*" : Action | undefined - - }; -} - -declare function createMachine( ->createMachine : (config: MachineConfig) => void ->type : string - - config: MachineConfig ->config : MachineConfig - -): void; - -createMachine({ ->createMachine({ schema: { events: {} as { type: "FOO" } | { type: "BAR" }, }, on: { FOO: (ev) => { ev.type; // should be 'FOO' }, },}) : void ->createMachine : (config: MachineConfig) => void ->{ schema: { events: {} as { type: "FOO" } | { type: "BAR" }, }, on: { FOO: (ev) => { ev.type; // should be 'FOO' }, },} : { schema: { events: { type: "FOO"; } | { type: "BAR"; }; }; on: { FOO: (ev: { type: "FOO"; }) => void; }; } - - schema: { ->schema : { events: { type: "FOO"; } | { type: "BAR"; }; } ->{ events: {} as { type: "FOO" } | { type: "BAR" }, } : { events: { type: "FOO"; } | { type: "BAR"; }; } - - events: {} as { type: "FOO" } | { type: "BAR" }, ->events : { type: "FOO"; } | { type: "BAR"; } ->{} as { type: "FOO" } | { type: "BAR" } : { type: "FOO"; } | { type: "BAR"; } ->{} : {} ->type : "FOO" ->type : "BAR" - - }, - on: { ->on : { FOO: (ev: { type: "FOO"; }) => void; } ->{ FOO: (ev) => { ev.type; // should be 'FOO' }, } : { FOO: (ev: { type: "FOO"; }) => void; } - - FOO: (ev) => { ->FOO : (ev: { type: "FOO"; }) => void ->(ev) => { ev.type; // should be 'FOO' } : (ev: { type: "FOO"; }) => void ->ev : { type: "FOO"; } - - ev.type; // should be 'FOO' ->ev.type : "FOO" ->ev : { type: "FOO"; } ->type : "FOO" - - }, - }, -}); - diff --git a/tests/cases/compiler/contextualTypeFunctionObjectPropertyIntersection.ts b/tests/cases/compiler/contextualTypeFunctionObjectPropertyIntersection.ts deleted file mode 100644 index 2d7e00f32f65e..0000000000000 --- a/tests/cases/compiler/contextualTypeFunctionObjectPropertyIntersection.ts +++ /dev/null @@ -1,29 +0,0 @@ -// @strict: true - -type Action = (ev: TEvent) => void; - -interface MachineConfig { - schema: { - events: TEvent; - }; - on?: { - [K in TEvent["type"]]?: Action; - } & { - "*"?: Action; - }; -} - -declare function createMachine( - config: MachineConfig -): void; - -createMachine({ - schema: { - events: {} as { type: "FOO" } | { type: "BAR" }, - }, - on: { - FOO: (ev) => { - ev.type; // should be 'FOO' - }, - }, -});