53 lines
3 KiB
Plaintext
53 lines
3 KiB
Plaintext
tests/cases/compiler/paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts(27,23): error TS2345: Argument of type '{ x?: number[] | undefined; y?: string[] | undefined; }' is not assignable to parameter of type '{ y?: number[] | undefined; }'.
|
|
Types of property 'y' are incompatible.
|
|
Type 'string[] | undefined' is not assignable to type 'number[] | undefined'.
|
|
Type 'string[]' is not assignable to type 'number[]'.
|
|
Type 'string' is not assignable to type 'number'.
|
|
tests/cases/compiler/paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts(28,23): error TS2345: Argument of type '{ x?: number[] | undefined; y?: string[] | undefined; }' is not assignable to parameter of type '{ x?: string[] | undefined; }'.
|
|
Types of property 'x' are incompatible.
|
|
Type 'number[] | undefined' is not assignable to type 'string[] | undefined'.
|
|
Type 'number[]' is not assignable to type 'string[]'.
|
|
Type 'number' is not assignable to type 'string'.
|
|
|
|
|
|
==== tests/cases/compiler/paramsOnlyHaveLiteralTypesWhenAppropriatelyContextualized.ts (2 errors) ====
|
|
// Using a homomorphic mapped type over `T`
|
|
// Produces a lower-priority inference for `T` than other
|
|
// positions, allowing one to override the priority the argument
|
|
// order would usually imply
|
|
type Lower<T> = { [K in keyof T]: T[K] };
|
|
|
|
export function appendToOptionalArray<
|
|
K extends string | number | symbol,
|
|
T
|
|
>(
|
|
object: { [x in K]?: Lower<T>[] },
|
|
key: K,
|
|
value: T
|
|
) {
|
|
const array = object[key];
|
|
if (array) {
|
|
array.push(value);
|
|
} else {
|
|
object[key] = [value];
|
|
}
|
|
}
|
|
|
|
// e.g.
|
|
const foo: {x?: number[]; y?: string[]; } = {};
|
|
appendToOptionalArray(foo, 'x', 123); // ok
|
|
appendToOptionalArray(foo, 'y', 'bar'); // ok
|
|
appendToOptionalArray(foo, 'y', 12); // should fail
|
|
~~~
|
|
!!! error TS2345: Argument of type '{ x?: number[] | undefined; y?: string[] | undefined; }' is not assignable to parameter of type '{ y?: number[] | undefined; }'.
|
|
!!! error TS2345: Types of property 'y' are incompatible.
|
|
!!! error TS2345: Type 'string[] | undefined' is not assignable to type 'number[] | undefined'.
|
|
!!! error TS2345: Type 'string[]' is not assignable to type 'number[]'.
|
|
!!! error TS2345: Type 'string' is not assignable to type 'number'.
|
|
appendToOptionalArray(foo, 'x', "no"); // should fail
|
|
~~~
|
|
!!! error TS2345: Argument of type '{ x?: number[] | undefined; y?: string[] | undefined; }' is not assignable to parameter of type '{ x?: string[] | undefined; }'.
|
|
!!! error TS2345: Types of property 'x' are incompatible.
|
|
!!! error TS2345: Type 'number[] | undefined' is not assignable to type 'string[] | undefined'.
|
|
!!! error TS2345: Type 'number[]' is not assignable to type 'string[]'.
|
|
!!! error TS2345: Type 'number' is not assignable to type 'string'. |