|
| 1 | +=== tests/cases/compiler/paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts === |
| 2 | +// Using a homomorphic mapped type over `T` |
| 3 | +// Produces a lower-priority inference for `T` than other |
| 4 | +// positions, allowing one to override the priority the argument |
| 5 | +// order would usually imply |
| 6 | +type Lower<T> = { [K in keyof T]: T[K] }; |
| 7 | +>Lower : Symbol(Lower, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 0, 0)) |
| 8 | +>T : Symbol(T, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 4, 11)) |
| 9 | +>K : Symbol(K, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 4, 19)) |
| 10 | +>T : Symbol(T, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 4, 11)) |
| 11 | +>T : Symbol(T, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 4, 11)) |
| 12 | +>K : Symbol(K, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 4, 19)) |
| 13 | + |
| 14 | +export function appendToOptionalArray< |
| 15 | +>appendToOptionalArray : Symbol(appendToOptionalArray, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 4, 41)) |
| 16 | + |
| 17 | + K extends string | number | symbol, |
| 18 | +>K : Symbol(K, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 6, 38)) |
| 19 | + |
| 20 | + T |
| 21 | +>T : Symbol(T, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 7, 37)) |
| 22 | + |
| 23 | +>( |
| 24 | + object: { [x in K]?: Lower<T>[] }, |
| 25 | +>object : Symbol(object, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 9, 2)) |
| 26 | +>x : Symbol(x, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 10, 13)) |
| 27 | +>K : Symbol(K, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 6, 38)) |
| 28 | +>Lower : Symbol(Lower, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 0, 0)) |
| 29 | +>T : Symbol(T, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 7, 37)) |
| 30 | + |
| 31 | + key: K, |
| 32 | +>key : Symbol(key, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 10, 36)) |
| 33 | +>K : Symbol(K, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 6, 38)) |
| 34 | + |
| 35 | + value: T |
| 36 | +>value : Symbol(value, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 11, 9)) |
| 37 | +>T : Symbol(T, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 7, 37)) |
| 38 | + |
| 39 | +) { |
| 40 | + const array = object[key]; |
| 41 | +>array : Symbol(array, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 14, 7)) |
| 42 | +>object : Symbol(object, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 9, 2)) |
| 43 | +>key : Symbol(key, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 10, 36)) |
| 44 | + |
| 45 | + if (array) { |
| 46 | +>array : Symbol(array, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 14, 7)) |
| 47 | + |
| 48 | + array.push(value); |
| 49 | +>array.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --)) |
| 50 | +>array : Symbol(array, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 14, 7)) |
| 51 | +>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --)) |
| 52 | +>value : Symbol(value, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 11, 9)) |
| 53 | + |
| 54 | + } else { |
| 55 | + object[key] = [value]; |
| 56 | +>object : Symbol(object, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 9, 2)) |
| 57 | +>key : Symbol(key, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 10, 36)) |
| 58 | +>value : Symbol(value, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 11, 9)) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// e.g. |
| 63 | +const foo: {x?: number[]; y?: string[]; } = {}; |
| 64 | +>foo : Symbol(foo, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 23, 5)) |
| 65 | +>x : Symbol(x, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 23, 12)) |
| 66 | +>y : Symbol(y, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 23, 25)) |
| 67 | + |
| 68 | +appendToOptionalArray(foo, 'x', 123); // ok |
| 69 | +>appendToOptionalArray : Symbol(appendToOptionalArray, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 4, 41)) |
| 70 | +>foo : Symbol(foo, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 23, 5)) |
| 71 | + |
| 72 | +appendToOptionalArray(foo, 'y', 'bar'); // ok |
| 73 | +>appendToOptionalArray : Symbol(appendToOptionalArray, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 4, 41)) |
| 74 | +>foo : Symbol(foo, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 23, 5)) |
| 75 | + |
| 76 | +appendToOptionalArray(foo, 'y', 12); // should fail |
| 77 | +>appendToOptionalArray : Symbol(appendToOptionalArray, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 4, 41)) |
| 78 | +>foo : Symbol(foo, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 23, 5)) |
| 79 | + |
| 80 | +appendToOptionalArray(foo, 'x', "no"); // should fail |
| 81 | +>appendToOptionalArray : Symbol(appendToOptionalArray, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 4, 41)) |
| 82 | +>foo : Symbol(foo, Decl(paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts, 23, 5)) |
| 83 | + |
0 commit comments