TypeScript/tests/cases/compiler/emptyObjectNotSubtypeOfIndexSignatureContainingObject1.ts
Wesley Wigham 2533d8294e
Make a fresh empty object literal not a subtype of a type with an index signaure (#29975)
* Forbid inferable index checkign during subtype relationship checking

* Merge object.values and object.entries overloads to work around subtype change

* Invert subtype relationship between fresh empty objects and non-empty object types

* Remvoe comment

* Revert lib change

* Remove trailing whitespace ffs
2019-02-26 13:43:22 -08:00

43 lines
1.1 KiB
TypeScript

// This should behave the same as emptyObjectNotSubtypeOfIndexSignatureContainingObject2.ts
// Begin types from Lodash.
interface Dictionary<T> {
[index: string]: T;
}
interface NumericDictionary<T> {
[index: number]: T;
}
type ObjectIterator<TObject, TResult> = (
value: TObject[keyof TObject],
key: string,
collection: TObject
) => TResult;
type DictionaryIterator<T, TResult> = ObjectIterator<Dictionary<T>, TResult>;
// In lodash.d.ts this function has many overloads, but this seems to be the problematic one.
function mapValues<T, TResult>(
obj: Dictionary<T> | NumericDictionary<T> | null | undefined,
callback: DictionaryIterator<T, TResult>
): Dictionary<TResult> {
return null as any;
}
// End types from Lodash.
interface Foo {
foo: string;
}
interface Bar {
bar: string;
}
export function fooToBar(
foos: Record<string, Foo>
): Record<string, Bar | null> {
const result = foos == null ? {} : mapValues(foos, f => f.foo);
// This line _should_ fail, because `result` is not the right type.
return result;
}