From ef5f671fe90f60fad740baa6476716ccb6135ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Mon, 6 Mar 2023 13:03:50 +0100 Subject: [PATCH] Add extra test cases for indexed access on generic mapped type --- tests/baselines/reference/correlatedUnions.js | 51 ++++++++++- .../reference/correlatedUnions.symbols | 86 +++++++++++++++++++ .../reference/correlatedUnions.types | 70 +++++++++++++++ tests/cases/compiler/correlatedUnions.ts | 32 ++++++- 4 files changed, 237 insertions(+), 2 deletions(-) diff --git a/tests/baselines/reference/correlatedUnions.js b/tests/baselines/reference/correlatedUnions.js index dbcc6db949cde..6a45b42210c14 100644 --- a/tests/baselines/reference/correlatedUnions.js +++ b/tests/baselines/reference/correlatedUnions.js @@ -268,7 +268,38 @@ const getStringAndNumberFromOriginalAndMapped = < nestedKey: N ): [Original[K][N], MappedFromOriginal[K][N]] => { return [original[key][nestedKey], mappedFromOriginal[key][nestedKey]]; -}; +}; + +// repro from #31675 +interface Config { + string: string; + number: number; +} + +function getConfigOrDefault( + userConfig: Partial, + key: T, + defaultValue: Config[T] +): Config[T] { + const userValue = userConfig[key]; + const assertedCheck = userValue ? userValue! : defaultValue; + return assertedCheck; +} + +// repro from #47523 + +type Foo1 = { + x: number; + y: string; +}; + +function getValueConcrete( + o: Partial, + k: K +): Foo1[K] | undefined { + return o[k]; +} + //// [correlatedUnions.js] "use strict"; @@ -392,6 +423,14 @@ var BAR_LOOKUP = makeCompleteLookupMapping(ALL_BARS, 'name'); var getStringAndNumberFromOriginalAndMapped = function (original, mappedFromOriginal, key, nestedKey) { return [original[key][nestedKey], mappedFromOriginal[key][nestedKey]]; }; +function getConfigOrDefault(userConfig, key, defaultValue) { + var userValue = userConfig[key]; + var assertedCheck = userValue ? userValue : defaultValue; + return assertedCheck; +} +function getValueConcrete(o, k) { + return o[k]; +} //// [correlatedUnions.d.ts] @@ -562,3 +601,13 @@ type SameKeys = { }; type MappedFromOriginal = SameKeys; declare const getStringAndNumberFromOriginalAndMapped: (original: Original, mappedFromOriginal: MappedFromOriginal, key: K, nestedKey: N) => [Original[K][N], SameKeys[K][N]]; +interface Config { + string: string; + number: number; +} +declare function getConfigOrDefault(userConfig: Partial, key: T, defaultValue: Config[T]): Config[T]; +type Foo1 = { + x: number; + y: string; +}; +declare function getValueConcrete(o: Partial, k: K): Foo1[K] | undefined; diff --git a/tests/baselines/reference/correlatedUnions.symbols b/tests/baselines/reference/correlatedUnions.symbols index c1948aef3eef6..97a5f60c8748f 100644 --- a/tests/baselines/reference/correlatedUnions.symbols +++ b/tests/baselines/reference/correlatedUnions.symbols @@ -928,3 +928,89 @@ const getStringAndNumberFromOriginalAndMapped = < >nestedKey : Symbol(nestedKey, Decl(correlatedUnions.ts, 265, 9)) }; + +// repro from #31675 +interface Config { +>Config : Symbol(Config, Decl(correlatedUnions.ts, 269, 2)) + + string: string; +>string : Symbol(Config.string, Decl(correlatedUnions.ts, 272, 18)) + + number: number; +>number : Symbol(Config.number, Decl(correlatedUnions.ts, 273, 17)) +} + +function getConfigOrDefault( +>getConfigOrDefault : Symbol(getConfigOrDefault, Decl(correlatedUnions.ts, 275, 1)) +>T : Symbol(T, Decl(correlatedUnions.ts, 277, 28)) +>Config : Symbol(Config, Decl(correlatedUnions.ts, 269, 2)) + + userConfig: Partial, +>userConfig : Symbol(userConfig, Decl(correlatedUnions.ts, 277, 52)) +>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --)) +>Config : Symbol(Config, Decl(correlatedUnions.ts, 269, 2)) + + key: T, +>key : Symbol(key, Decl(correlatedUnions.ts, 278, 30)) +>T : Symbol(T, Decl(correlatedUnions.ts, 277, 28)) + + defaultValue: Config[T] +>defaultValue : Symbol(defaultValue, Decl(correlatedUnions.ts, 279, 9)) +>Config : Symbol(Config, Decl(correlatedUnions.ts, 269, 2)) +>T : Symbol(T, Decl(correlatedUnions.ts, 277, 28)) + +): Config[T] { +>Config : Symbol(Config, Decl(correlatedUnions.ts, 269, 2)) +>T : Symbol(T, Decl(correlatedUnions.ts, 277, 28)) + + const userValue = userConfig[key]; +>userValue : Symbol(userValue, Decl(correlatedUnions.ts, 282, 7)) +>userConfig : Symbol(userConfig, Decl(correlatedUnions.ts, 277, 52)) +>key : Symbol(key, Decl(correlatedUnions.ts, 278, 30)) + + const assertedCheck = userValue ? userValue! : defaultValue; +>assertedCheck : Symbol(assertedCheck, Decl(correlatedUnions.ts, 283, 7)) +>userValue : Symbol(userValue, Decl(correlatedUnions.ts, 282, 7)) +>userValue : Symbol(userValue, Decl(correlatedUnions.ts, 282, 7)) +>defaultValue : Symbol(defaultValue, Decl(correlatedUnions.ts, 279, 9)) + + return assertedCheck; +>assertedCheck : Symbol(assertedCheck, Decl(correlatedUnions.ts, 283, 7)) +} + +// repro from #47523 + +type Foo1 = { +>Foo1 : Symbol(Foo1, Decl(correlatedUnions.ts, 285, 1)) + + x: number; +>x : Symbol(x, Decl(correlatedUnions.ts, 289, 13)) + + y: string; +>y : Symbol(y, Decl(correlatedUnions.ts, 290, 12)) + +}; + +function getValueConcrete( +>getValueConcrete : Symbol(getValueConcrete, Decl(correlatedUnions.ts, 292, 2)) +>K : Symbol(K, Decl(correlatedUnions.ts, 294, 26)) +>Foo1 : Symbol(Foo1, Decl(correlatedUnions.ts, 285, 1)) + + o: Partial, +>o : Symbol(o, Decl(correlatedUnions.ts, 294, 48)) +>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --)) +>Foo1 : Symbol(Foo1, Decl(correlatedUnions.ts, 285, 1)) + + k: K +>k : Symbol(k, Decl(correlatedUnions.ts, 295, 19)) +>K : Symbol(K, Decl(correlatedUnions.ts, 294, 26)) + +): Foo1[K] | undefined { +>Foo1 : Symbol(Foo1, Decl(correlatedUnions.ts, 285, 1)) +>K : Symbol(K, Decl(correlatedUnions.ts, 294, 26)) + + return o[k]; +>o : Symbol(o, Decl(correlatedUnions.ts, 294, 48)) +>k : Symbol(k, Decl(correlatedUnions.ts, 295, 19)) +} + diff --git a/tests/baselines/reference/correlatedUnions.types b/tests/baselines/reference/correlatedUnions.types index 496dbc91eb4a9..4f80a4250d1e2 100644 --- a/tests/baselines/reference/correlatedUnions.types +++ b/tests/baselines/reference/correlatedUnions.types @@ -828,3 +828,73 @@ const getStringAndNumberFromOriginalAndMapped = < >nestedKey : N }; + +// repro from #31675 +interface Config { + string: string; +>string : string + + number: number; +>number : number +} + +function getConfigOrDefault( +>getConfigOrDefault : (userConfig: Partial, key: T, defaultValue: Config[T]) => Config[T] + + userConfig: Partial, +>userConfig : Partial + + key: T, +>key : T + + defaultValue: Config[T] +>defaultValue : Config[T] + +): Config[T] { + const userValue = userConfig[key]; +>userValue : Partial[T] +>userConfig[key] : Partial[T] +>userConfig : Partial +>key : T + + const assertedCheck = userValue ? userValue! : defaultValue; +>assertedCheck : Config[T] +>userValue ? userValue! : defaultValue : Config[T] +>userValue : Partial[T] +>userValue! : NonNullable[T]> +>userValue : NonNullable[T]> +>defaultValue : Config[T] + + return assertedCheck; +>assertedCheck : Config[T] +} + +// repro from #47523 + +type Foo1 = { +>Foo1 : { x: number; y: string; } + + x: number; +>x : number + + y: string; +>y : string + +}; + +function getValueConcrete( +>getValueConcrete : (o: Partial, k: K) => Foo1[K] | undefined + + o: Partial, +>o : Partial + + k: K +>k : K + +): Foo1[K] | undefined { + return o[k]; +>o[k] : Partial[K] +>o : Partial +>k : K +} + diff --git a/tests/cases/compiler/correlatedUnions.ts b/tests/cases/compiler/correlatedUnions.ts index 26ceee428de27..460ddf408a104 100644 --- a/tests/cases/compiler/correlatedUnions.ts +++ b/tests/cases/compiler/correlatedUnions.ts @@ -270,4 +270,34 @@ const getStringAndNumberFromOriginalAndMapped = < nestedKey: N ): [Original[K][N], MappedFromOriginal[K][N]] => { return [original[key][nestedKey], mappedFromOriginal[key][nestedKey]]; -}; \ No newline at end of file +}; + +// repro from #31675 +interface Config { + string: string; + number: number; +} + +function getConfigOrDefault( + userConfig: Partial, + key: T, + defaultValue: Config[T] +): Config[T] { + const userValue = userConfig[key]; + const assertedCheck = userValue ? userValue! : defaultValue; + return assertedCheck; +} + +// repro from #47523 + +type Foo1 = { + x: number; + y: string; +}; + +function getValueConcrete( + o: Partial, + k: K +): Foo1[K] | undefined { + return o[k]; +}