From f73308b248d99a30a741314e2a8e5aba9415e311 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 2 May 2019 15:24:56 -0700 Subject: [PATCH] Add tests --- .../reverseMappedPartiallyInferableTypes.ts | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 tests/cases/compiler/reverseMappedPartiallyInferableTypes.ts diff --git a/tests/cases/compiler/reverseMappedPartiallyInferableTypes.ts b/tests/cases/compiler/reverseMappedPartiallyInferableTypes.ts new file mode 100644 index 000000000000..799ff641c5a4 --- /dev/null +++ b/tests/cases/compiler/reverseMappedPartiallyInferableTypes.ts @@ -0,0 +1,96 @@ +// @strict: true + +// Repro from #30505 + +export type Prop = { (): T } +export type PropType = Prop; +export type PropDefaultValue = T; + + +export type PropValidatorFunction = (value: T) => boolean; +export type PropValidator = PropOptions; + + +export type PropOptions = { + type: PropType; + + value?: PropDefaultValue, + required?: boolean; + validator?: PropValidatorFunction; +} + +export type RecordPropsDefinition = { + [K in keyof T]: PropValidator +} +export type PropsDefinition = RecordPropsDefinition; + + +declare function extend({ props }: { props: PropsDefinition }): PropsDefinition; + +interface MyType { + valid: boolean; +} + +const r = extend({ + props: { + notResolved: { + type: Object as PropType, + validator: x => { + return x.valid; + } + }, + explicit: { + type: Object as PropType, + validator: (x: MyType) => { + return x.valid; + } + } + } +}) + +r.explicit +r.notResolved +r.explicit.required +r.notResolved.required + +// Modified repro from #30505 + +type Box = { + contents?: T; + contains?(content: T): boolean; +}; + +type Mapped = { + [K in keyof T]: Box; +} + +declare function id(arg: Mapped): Mapped; + +// All properties have inferable types + +const obj1 = id({ + foo: { + contents: "" + } +}); + +// Some properties have inferable types + +const obj2 = id({ + foo: { + contents: "", + contains(k) { + return k.length > 0; + } + } +}); + +// No properties have inferable types + +const obj3 = id({ + foo: { + contains(k) { + return k.length > 0; + } + } +});