mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-12 04:17:34 -06:00
Merge pull request #31221 from microsoft/improveReverseMappedTypes
Improve reverse mapped types
This commit is contained in:
commit
ae3d1d45c1
@ -14921,10 +14921,19 @@ namespace ts {
|
||||
return type;
|
||||
}
|
||||
|
||||
// We consider a type to be partially inferable if it isn't marked non-inferable or if it is
|
||||
// an object literal type with at least one property of an inferable type. For example, an object
|
||||
// literal { a: 123, b: x => true } is marked non-inferable because it contains a context sensitive
|
||||
// arrow function, but is considered partially inferable because property 'a' has an inferable type.
|
||||
function isPartiallyInferableType(type: Type): boolean {
|
||||
return !(getObjectFlags(type) & ObjectFlags.NonInferrableType) ||
|
||||
isObjectLiteralType(type) && some(getPropertiesOfType(type), prop => isPartiallyInferableType(getTypeOfSymbol(prop)));
|
||||
}
|
||||
|
||||
function createReverseMappedType(source: Type, target: MappedType, constraint: IndexType) {
|
||||
// If any property contains context sensitive functions that have been skipped, the source type
|
||||
// is incomplete and we can't infer a meaningful input type.
|
||||
if (getObjectFlags(source) & ObjectFlags.NonInferrableType || getPropertiesOfType(source).length === 0 && !getIndexInfoOfType(source, IndexKind.String)) {
|
||||
// We consider a source type reverse mappable if it has a string index signature or if
|
||||
// it has one or more properties and is of a partially inferable type.
|
||||
if (!(getIndexInfoOfType(source, IndexKind.String) || getPropertiesOfType(source).length !== 0 && isPartiallyInferableType(source))) {
|
||||
return undefined;
|
||||
}
|
||||
// For arrays and tuples we infer new arrays and tuples where the reverse mapping has been
|
||||
@ -15309,7 +15318,11 @@ namespace ts {
|
||||
const inferredType = inferTypeForHomomorphicMappedType(source, target, <IndexType>constraintType);
|
||||
if (inferredType) {
|
||||
const savePriority = priority;
|
||||
priority |= InferencePriority.HomomorphicMappedType;
|
||||
// We assign a lower priority to inferences made from types containing non-inferrable
|
||||
// types because we may only have a partial result (i.e. we may have failed to make
|
||||
// reverse inferences for some properties).
|
||||
priority |= getObjectFlags(source) & ObjectFlags.NonInferrableType ?
|
||||
InferencePriority.PartialHomomorphicMappedType : InferencePriority.HomomorphicMappedType;
|
||||
inferFromTypes(inferredType, inference.typeParameter);
|
||||
priority = savePriority;
|
||||
}
|
||||
|
||||
@ -4424,15 +4424,16 @@ namespace ts {
|
||||
export type TypeMapper = (t: TypeParameter) => Type;
|
||||
|
||||
export const enum InferencePriority {
|
||||
NakedTypeVariable = 1 << 0, // Naked type variable in union or intersection type
|
||||
HomomorphicMappedType = 1 << 1, // Reverse inference for homomorphic mapped type
|
||||
MappedTypeConstraint = 1 << 2, // Reverse inference for mapped type
|
||||
ReturnType = 1 << 3, // Inference made from return type of generic function
|
||||
LiteralKeyof = 1 << 4, // Inference made from a string literal to a keyof T
|
||||
NoConstraints = 1 << 5, // Don't infer from constraints of instantiable types
|
||||
AlwaysStrict = 1 << 6, // Always use strict rules for contravariant inferences
|
||||
NakedTypeVariable = 1 << 0, // Naked type variable in union or intersection type
|
||||
HomomorphicMappedType = 1 << 1, // Reverse inference for homomorphic mapped type
|
||||
PartialHomomorphicMappedType = 1 << 2, // Partial reverse inference for homomorphic mapped type
|
||||
MappedTypeConstraint = 1 << 3, // Reverse inference for mapped type
|
||||
ReturnType = 1 << 4, // Inference made from return type of generic function
|
||||
LiteralKeyof = 1 << 5, // Inference made from a string literal to a keyof T
|
||||
NoConstraints = 1 << 6, // Don't infer from constraints of instantiable types
|
||||
AlwaysStrict = 1 << 7, // Always use strict rules for contravariant inferences
|
||||
|
||||
PriorityImpliesCombination = ReturnType | MappedTypeConstraint | LiteralKeyof, // These priorities imply that the resulting type should be a combination of all candidates
|
||||
PriorityImpliesCombination = ReturnType | MappedTypeConstraint | LiteralKeyof, // These priorities imply that the resulting type should be a combination of all candidates
|
||||
}
|
||||
|
||||
/* @internal */
|
||||
|
||||
@ -2418,12 +2418,13 @@ declare namespace ts {
|
||||
enum InferencePriority {
|
||||
NakedTypeVariable = 1,
|
||||
HomomorphicMappedType = 2,
|
||||
MappedTypeConstraint = 4,
|
||||
ReturnType = 8,
|
||||
LiteralKeyof = 16,
|
||||
NoConstraints = 32,
|
||||
AlwaysStrict = 64,
|
||||
PriorityImpliesCombination = 28
|
||||
PartialHomomorphicMappedType = 4,
|
||||
MappedTypeConstraint = 8,
|
||||
ReturnType = 16,
|
||||
LiteralKeyof = 32,
|
||||
NoConstraints = 64,
|
||||
AlwaysStrict = 128,
|
||||
PriorityImpliesCombination = 56
|
||||
}
|
||||
/** @deprecated Use FileExtensionInfo instead. */
|
||||
type JsFileExtensionInfo = FileExtensionInfo;
|
||||
|
||||
13
tests/baselines/reference/api/typescript.d.ts
vendored
13
tests/baselines/reference/api/typescript.d.ts
vendored
@ -2418,12 +2418,13 @@ declare namespace ts {
|
||||
enum InferencePriority {
|
||||
NakedTypeVariable = 1,
|
||||
HomomorphicMappedType = 2,
|
||||
MappedTypeConstraint = 4,
|
||||
ReturnType = 8,
|
||||
LiteralKeyof = 16,
|
||||
NoConstraints = 32,
|
||||
AlwaysStrict = 64,
|
||||
PriorityImpliesCombination = 28
|
||||
PartialHomomorphicMappedType = 4,
|
||||
MappedTypeConstraint = 8,
|
||||
ReturnType = 16,
|
||||
LiteralKeyof = 32,
|
||||
NoConstraints = 64,
|
||||
AlwaysStrict = 128,
|
||||
PriorityImpliesCombination = 56
|
||||
}
|
||||
/** @deprecated Use FileExtensionInfo instead. */
|
||||
type JsFileExtensionInfo = FileExtensionInfo;
|
||||
|
||||
@ -20,7 +20,7 @@ tests/cases/conformance/types/mapped/mappedTypeInferenceErrors.ts(16,9): error T
|
||||
baz: 42
|
||||
~~~
|
||||
!!! error TS2322: Type 'number' is not assignable to type '() => unknown'.
|
||||
!!! related TS6500 tests/cases/conformance/types/mapped/mappedTypeInferenceErrors.ts:16:9: The expected type comes from property 'baz' which is declared here on type 'ComputedOf<{ bar: number; baz: unknown; }>'
|
||||
!!! related TS6500 tests/cases/conformance/types/mapped/mappedTypeInferenceErrors.ts:16:9: The expected type comes from property 'baz' which is declared here on type 'ComputedOf<{ bar: unknown; baz: unknown; }>'
|
||||
}
|
||||
});
|
||||
|
||||
@ -0,0 +1,101 @@
|
||||
tests/cases/compiler/reverseMappedPartiallyInferableTypes.ts(91,20): error TS2571: Object is of type 'unknown'.
|
||||
|
||||
|
||||
==== tests/cases/compiler/reverseMappedPartiallyInferableTypes.ts (1 errors) ====
|
||||
// Repro from #30505
|
||||
|
||||
export type Prop<T> = { (): T }
|
||||
export type PropType<T> = Prop<T>;
|
||||
export type PropDefaultValue<T> = T;
|
||||
|
||||
|
||||
export type PropValidatorFunction<T> = (value: T) => boolean;
|
||||
export type PropValidator<T> = PropOptions<T>;
|
||||
|
||||
|
||||
export type PropOptions<T> = {
|
||||
type: PropType<T>;
|
||||
|
||||
value?: PropDefaultValue<T>,
|
||||
required?: boolean;
|
||||
validator?: PropValidatorFunction<T>;
|
||||
}
|
||||
|
||||
export type RecordPropsDefinition<T> = {
|
||||
[K in keyof T]: PropValidator<T[K]>
|
||||
}
|
||||
export type PropsDefinition<T> = RecordPropsDefinition<T>;
|
||||
|
||||
|
||||
declare function extend<T>({ props }: { props: PropsDefinition<T> }): PropsDefinition<T>;
|
||||
|
||||
interface MyType {
|
||||
valid: boolean;
|
||||
}
|
||||
|
||||
const r = extend({
|
||||
props: {
|
||||
notResolved: {
|
||||
type: Object as PropType<MyType>,
|
||||
validator: x => {
|
||||
return x.valid;
|
||||
}
|
||||
},
|
||||
explicit: {
|
||||
type: Object as PropType<MyType>,
|
||||
validator: (x: MyType) => {
|
||||
return x.valid;
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
r.explicit
|
||||
r.notResolved
|
||||
r.explicit.required
|
||||
r.notResolved.required
|
||||
|
||||
// Modified repro from #30505
|
||||
|
||||
type Box<T> = {
|
||||
contents?: T;
|
||||
contains?(content: T): boolean;
|
||||
};
|
||||
|
||||
type Mapped<T> = {
|
||||
[K in keyof T]: Box<T[K]>;
|
||||
}
|
||||
|
||||
declare function id<T>(arg: Mapped<T>): Mapped<T>;
|
||||
|
||||
// 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;
|
||||
~
|
||||
!!! error TS2571: Object is of type 'unknown'.
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@ -0,0 +1,144 @@
|
||||
//// [reverseMappedPartiallyInferableTypes.ts]
|
||||
// Repro from #30505
|
||||
|
||||
export type Prop<T> = { (): T }
|
||||
export type PropType<T> = Prop<T>;
|
||||
export type PropDefaultValue<T> = T;
|
||||
|
||||
|
||||
export type PropValidatorFunction<T> = (value: T) => boolean;
|
||||
export type PropValidator<T> = PropOptions<T>;
|
||||
|
||||
|
||||
export type PropOptions<T> = {
|
||||
type: PropType<T>;
|
||||
|
||||
value?: PropDefaultValue<T>,
|
||||
required?: boolean;
|
||||
validator?: PropValidatorFunction<T>;
|
||||
}
|
||||
|
||||
export type RecordPropsDefinition<T> = {
|
||||
[K in keyof T]: PropValidator<T[K]>
|
||||
}
|
||||
export type PropsDefinition<T> = RecordPropsDefinition<T>;
|
||||
|
||||
|
||||
declare function extend<T>({ props }: { props: PropsDefinition<T> }): PropsDefinition<T>;
|
||||
|
||||
interface MyType {
|
||||
valid: boolean;
|
||||
}
|
||||
|
||||
const r = extend({
|
||||
props: {
|
||||
notResolved: {
|
||||
type: Object as PropType<MyType>,
|
||||
validator: x => {
|
||||
return x.valid;
|
||||
}
|
||||
},
|
||||
explicit: {
|
||||
type: Object as PropType<MyType>,
|
||||
validator: (x: MyType) => {
|
||||
return x.valid;
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
r.explicit
|
||||
r.notResolved
|
||||
r.explicit.required
|
||||
r.notResolved.required
|
||||
|
||||
// Modified repro from #30505
|
||||
|
||||
type Box<T> = {
|
||||
contents?: T;
|
||||
contains?(content: T): boolean;
|
||||
};
|
||||
|
||||
type Mapped<T> = {
|
||||
[K in keyof T]: Box<T[K]>;
|
||||
}
|
||||
|
||||
declare function id<T>(arg: Mapped<T>): Mapped<T>;
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
//// [reverseMappedPartiallyInferableTypes.js]
|
||||
"use strict";
|
||||
// Repro from #30505
|
||||
exports.__esModule = true;
|
||||
var r = extend({
|
||||
props: {
|
||||
notResolved: {
|
||||
type: Object,
|
||||
validator: function (x) {
|
||||
return x.valid;
|
||||
}
|
||||
},
|
||||
explicit: {
|
||||
type: Object,
|
||||
validator: function (x) {
|
||||
return x.valid;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
r.explicit;
|
||||
r.notResolved;
|
||||
r.explicit.required;
|
||||
r.notResolved.required;
|
||||
// All properties have inferable types
|
||||
var obj1 = id({
|
||||
foo: {
|
||||
contents: ""
|
||||
}
|
||||
});
|
||||
// Some properties have inferable types
|
||||
var obj2 = id({
|
||||
foo: {
|
||||
contents: "",
|
||||
contains: function (k) {
|
||||
return k.length > 0;
|
||||
}
|
||||
}
|
||||
});
|
||||
// No properties have inferable types
|
||||
var obj3 = id({
|
||||
foo: {
|
||||
contains: function (k) {
|
||||
return k.length > 0;
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,259 @@
|
||||
=== tests/cases/compiler/reverseMappedPartiallyInferableTypes.ts ===
|
||||
// Repro from #30505
|
||||
|
||||
export type Prop<T> = { (): T }
|
||||
>Prop : Symbol(Prop, Decl(reverseMappedPartiallyInferableTypes.ts, 0, 0))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 2, 17))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 2, 17))
|
||||
|
||||
export type PropType<T> = Prop<T>;
|
||||
>PropType : Symbol(PropType, Decl(reverseMappedPartiallyInferableTypes.ts, 2, 31))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 3, 21))
|
||||
>Prop : Symbol(Prop, Decl(reverseMappedPartiallyInferableTypes.ts, 0, 0))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 3, 21))
|
||||
|
||||
export type PropDefaultValue<T> = T;
|
||||
>PropDefaultValue : Symbol(PropDefaultValue, Decl(reverseMappedPartiallyInferableTypes.ts, 3, 34))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 4, 29))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 4, 29))
|
||||
|
||||
|
||||
export type PropValidatorFunction<T> = (value: T) => boolean;
|
||||
>PropValidatorFunction : Symbol(PropValidatorFunction, Decl(reverseMappedPartiallyInferableTypes.ts, 4, 36))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 7, 34))
|
||||
>value : Symbol(value, Decl(reverseMappedPartiallyInferableTypes.ts, 7, 40))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 7, 34))
|
||||
|
||||
export type PropValidator<T> = PropOptions<T>;
|
||||
>PropValidator : Symbol(PropValidator, Decl(reverseMappedPartiallyInferableTypes.ts, 7, 61))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 8, 26))
|
||||
>PropOptions : Symbol(PropOptions, Decl(reverseMappedPartiallyInferableTypes.ts, 8, 46))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 8, 26))
|
||||
|
||||
|
||||
export type PropOptions<T> = {
|
||||
>PropOptions : Symbol(PropOptions, Decl(reverseMappedPartiallyInferableTypes.ts, 8, 46))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 11, 24))
|
||||
|
||||
type: PropType<T>;
|
||||
>type : Symbol(type, Decl(reverseMappedPartiallyInferableTypes.ts, 11, 30))
|
||||
>PropType : Symbol(PropType, Decl(reverseMappedPartiallyInferableTypes.ts, 2, 31))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 11, 24))
|
||||
|
||||
value?: PropDefaultValue<T>,
|
||||
>value : Symbol(value, Decl(reverseMappedPartiallyInferableTypes.ts, 12, 22))
|
||||
>PropDefaultValue : Symbol(PropDefaultValue, Decl(reverseMappedPartiallyInferableTypes.ts, 3, 34))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 11, 24))
|
||||
|
||||
required?: boolean;
|
||||
>required : Symbol(required, Decl(reverseMappedPartiallyInferableTypes.ts, 14, 32))
|
||||
|
||||
validator?: PropValidatorFunction<T>;
|
||||
>validator : Symbol(validator, Decl(reverseMappedPartiallyInferableTypes.ts, 15, 23))
|
||||
>PropValidatorFunction : Symbol(PropValidatorFunction, Decl(reverseMappedPartiallyInferableTypes.ts, 4, 36))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 11, 24))
|
||||
}
|
||||
|
||||
export type RecordPropsDefinition<T> = {
|
||||
>RecordPropsDefinition : Symbol(RecordPropsDefinition, Decl(reverseMappedPartiallyInferableTypes.ts, 17, 1))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 19, 34))
|
||||
|
||||
[K in keyof T]: PropValidator<T[K]>
|
||||
>K : Symbol(K, Decl(reverseMappedPartiallyInferableTypes.ts, 20, 5))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 19, 34))
|
||||
>PropValidator : Symbol(PropValidator, Decl(reverseMappedPartiallyInferableTypes.ts, 7, 61))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 19, 34))
|
||||
>K : Symbol(K, Decl(reverseMappedPartiallyInferableTypes.ts, 20, 5))
|
||||
}
|
||||
export type PropsDefinition<T> = RecordPropsDefinition<T>;
|
||||
>PropsDefinition : Symbol(PropsDefinition, Decl(reverseMappedPartiallyInferableTypes.ts, 21, 1))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 22, 28))
|
||||
>RecordPropsDefinition : Symbol(RecordPropsDefinition, Decl(reverseMappedPartiallyInferableTypes.ts, 17, 1))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 22, 28))
|
||||
|
||||
|
||||
declare function extend<T>({ props }: { props: PropsDefinition<T> }): PropsDefinition<T>;
|
||||
>extend : Symbol(extend, Decl(reverseMappedPartiallyInferableTypes.ts, 22, 58))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 25, 24))
|
||||
>props : Symbol(props, Decl(reverseMappedPartiallyInferableTypes.ts, 25, 28))
|
||||
>props : Symbol(props, Decl(reverseMappedPartiallyInferableTypes.ts, 25, 39))
|
||||
>PropsDefinition : Symbol(PropsDefinition, Decl(reverseMappedPartiallyInferableTypes.ts, 21, 1))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 25, 24))
|
||||
>PropsDefinition : Symbol(PropsDefinition, Decl(reverseMappedPartiallyInferableTypes.ts, 21, 1))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 25, 24))
|
||||
|
||||
interface MyType {
|
||||
>MyType : Symbol(MyType, Decl(reverseMappedPartiallyInferableTypes.ts, 25, 90))
|
||||
|
||||
valid: boolean;
|
||||
>valid : Symbol(MyType.valid, Decl(reverseMappedPartiallyInferableTypes.ts, 27, 18))
|
||||
}
|
||||
|
||||
const r = extend({
|
||||
>r : Symbol(r, Decl(reverseMappedPartiallyInferableTypes.ts, 31, 5))
|
||||
>extend : Symbol(extend, Decl(reverseMappedPartiallyInferableTypes.ts, 22, 58))
|
||||
|
||||
props: {
|
||||
>props : Symbol(props, Decl(reverseMappedPartiallyInferableTypes.ts, 31, 18))
|
||||
|
||||
notResolved: {
|
||||
>notResolved : Symbol(notResolved, Decl(reverseMappedPartiallyInferableTypes.ts, 32, 12))
|
||||
|
||||
type: Object as PropType<MyType>,
|
||||
>type : Symbol(type, Decl(reverseMappedPartiallyInferableTypes.ts, 33, 22))
|
||||
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
|
||||
>PropType : Symbol(PropType, Decl(reverseMappedPartiallyInferableTypes.ts, 2, 31))
|
||||
>MyType : Symbol(MyType, Decl(reverseMappedPartiallyInferableTypes.ts, 25, 90))
|
||||
|
||||
validator: x => {
|
||||
>validator : Symbol(validator, Decl(reverseMappedPartiallyInferableTypes.ts, 34, 45))
|
||||
>x : Symbol(x, Decl(reverseMappedPartiallyInferableTypes.ts, 35, 22))
|
||||
|
||||
return x.valid;
|
||||
>x.valid : Symbol(MyType.valid, Decl(reverseMappedPartiallyInferableTypes.ts, 27, 18))
|
||||
>x : Symbol(x, Decl(reverseMappedPartiallyInferableTypes.ts, 35, 22))
|
||||
>valid : Symbol(MyType.valid, Decl(reverseMappedPartiallyInferableTypes.ts, 27, 18))
|
||||
}
|
||||
},
|
||||
explicit: {
|
||||
>explicit : Symbol(explicit, Decl(reverseMappedPartiallyInferableTypes.ts, 38, 10))
|
||||
|
||||
type: Object as PropType<MyType>,
|
||||
>type : Symbol(type, Decl(reverseMappedPartiallyInferableTypes.ts, 39, 19))
|
||||
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
|
||||
>PropType : Symbol(PropType, Decl(reverseMappedPartiallyInferableTypes.ts, 2, 31))
|
||||
>MyType : Symbol(MyType, Decl(reverseMappedPartiallyInferableTypes.ts, 25, 90))
|
||||
|
||||
validator: (x: MyType) => {
|
||||
>validator : Symbol(validator, Decl(reverseMappedPartiallyInferableTypes.ts, 40, 45))
|
||||
>x : Symbol(x, Decl(reverseMappedPartiallyInferableTypes.ts, 41, 24))
|
||||
>MyType : Symbol(MyType, Decl(reverseMappedPartiallyInferableTypes.ts, 25, 90))
|
||||
|
||||
return x.valid;
|
||||
>x.valid : Symbol(MyType.valid, Decl(reverseMappedPartiallyInferableTypes.ts, 27, 18))
|
||||
>x : Symbol(x, Decl(reverseMappedPartiallyInferableTypes.ts, 41, 24))
|
||||
>valid : Symbol(MyType.valid, Decl(reverseMappedPartiallyInferableTypes.ts, 27, 18))
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
r.explicit
|
||||
>r.explicit : Symbol(explicit, Decl(reverseMappedPartiallyInferableTypes.ts, 38, 10))
|
||||
>r : Symbol(r, Decl(reverseMappedPartiallyInferableTypes.ts, 31, 5))
|
||||
>explicit : Symbol(explicit, Decl(reverseMappedPartiallyInferableTypes.ts, 38, 10))
|
||||
|
||||
r.notResolved
|
||||
>r.notResolved : Symbol(notResolved, Decl(reverseMappedPartiallyInferableTypes.ts, 32, 12))
|
||||
>r : Symbol(r, Decl(reverseMappedPartiallyInferableTypes.ts, 31, 5))
|
||||
>notResolved : Symbol(notResolved, Decl(reverseMappedPartiallyInferableTypes.ts, 32, 12))
|
||||
|
||||
r.explicit.required
|
||||
>r.explicit.required : Symbol(required, Decl(reverseMappedPartiallyInferableTypes.ts, 14, 32))
|
||||
>r.explicit : Symbol(explicit, Decl(reverseMappedPartiallyInferableTypes.ts, 38, 10))
|
||||
>r : Symbol(r, Decl(reverseMappedPartiallyInferableTypes.ts, 31, 5))
|
||||
>explicit : Symbol(explicit, Decl(reverseMappedPartiallyInferableTypes.ts, 38, 10))
|
||||
>required : Symbol(required, Decl(reverseMappedPartiallyInferableTypes.ts, 14, 32))
|
||||
|
||||
r.notResolved.required
|
||||
>r.notResolved.required : Symbol(required, Decl(reverseMappedPartiallyInferableTypes.ts, 14, 32))
|
||||
>r.notResolved : Symbol(notResolved, Decl(reverseMappedPartiallyInferableTypes.ts, 32, 12))
|
||||
>r : Symbol(r, Decl(reverseMappedPartiallyInferableTypes.ts, 31, 5))
|
||||
>notResolved : Symbol(notResolved, Decl(reverseMappedPartiallyInferableTypes.ts, 32, 12))
|
||||
>required : Symbol(required, Decl(reverseMappedPartiallyInferableTypes.ts, 14, 32))
|
||||
|
||||
// Modified repro from #30505
|
||||
|
||||
type Box<T> = {
|
||||
>Box : Symbol(Box, Decl(reverseMappedPartiallyInferableTypes.ts, 51, 22))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 55, 9))
|
||||
|
||||
contents?: T;
|
||||
>contents : Symbol(contents, Decl(reverseMappedPartiallyInferableTypes.ts, 55, 15))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 55, 9))
|
||||
|
||||
contains?(content: T): boolean;
|
||||
>contains : Symbol(contains, Decl(reverseMappedPartiallyInferableTypes.ts, 56, 17))
|
||||
>content : Symbol(content, Decl(reverseMappedPartiallyInferableTypes.ts, 57, 14))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 55, 9))
|
||||
|
||||
};
|
||||
|
||||
type Mapped<T> = {
|
||||
>Mapped : Symbol(Mapped, Decl(reverseMappedPartiallyInferableTypes.ts, 58, 2))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 60, 12))
|
||||
|
||||
[K in keyof T]: Box<T[K]>;
|
||||
>K : Symbol(K, Decl(reverseMappedPartiallyInferableTypes.ts, 61, 5))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 60, 12))
|
||||
>Box : Symbol(Box, Decl(reverseMappedPartiallyInferableTypes.ts, 51, 22))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 60, 12))
|
||||
>K : Symbol(K, Decl(reverseMappedPartiallyInferableTypes.ts, 61, 5))
|
||||
}
|
||||
|
||||
declare function id<T>(arg: Mapped<T>): Mapped<T>;
|
||||
>id : Symbol(id, Decl(reverseMappedPartiallyInferableTypes.ts, 62, 1))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 64, 20))
|
||||
>arg : Symbol(arg, Decl(reverseMappedPartiallyInferableTypes.ts, 64, 23))
|
||||
>Mapped : Symbol(Mapped, Decl(reverseMappedPartiallyInferableTypes.ts, 58, 2))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 64, 20))
|
||||
>Mapped : Symbol(Mapped, Decl(reverseMappedPartiallyInferableTypes.ts, 58, 2))
|
||||
>T : Symbol(T, Decl(reverseMappedPartiallyInferableTypes.ts, 64, 20))
|
||||
|
||||
// All properties have inferable types
|
||||
|
||||
const obj1 = id({
|
||||
>obj1 : Symbol(obj1, Decl(reverseMappedPartiallyInferableTypes.ts, 68, 5))
|
||||
>id : Symbol(id, Decl(reverseMappedPartiallyInferableTypes.ts, 62, 1))
|
||||
|
||||
foo: {
|
||||
>foo : Symbol(foo, Decl(reverseMappedPartiallyInferableTypes.ts, 68, 17))
|
||||
|
||||
contents: ""
|
||||
>contents : Symbol(contents, Decl(reverseMappedPartiallyInferableTypes.ts, 69, 10))
|
||||
}
|
||||
});
|
||||
|
||||
// Some properties have inferable types
|
||||
|
||||
const obj2 = id({
|
||||
>obj2 : Symbol(obj2, Decl(reverseMappedPartiallyInferableTypes.ts, 76, 5))
|
||||
>id : Symbol(id, Decl(reverseMappedPartiallyInferableTypes.ts, 62, 1))
|
||||
|
||||
foo: {
|
||||
>foo : Symbol(foo, Decl(reverseMappedPartiallyInferableTypes.ts, 76, 17))
|
||||
|
||||
contents: "",
|
||||
>contents : Symbol(contents, Decl(reverseMappedPartiallyInferableTypes.ts, 77, 10))
|
||||
|
||||
contains(k) {
|
||||
>contains : Symbol(contains, Decl(reverseMappedPartiallyInferableTypes.ts, 78, 21))
|
||||
>k : Symbol(k, Decl(reverseMappedPartiallyInferableTypes.ts, 79, 17))
|
||||
|
||||
return k.length > 0;
|
||||
>k.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
|
||||
>k : Symbol(k, Decl(reverseMappedPartiallyInferableTypes.ts, 79, 17))
|
||||
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// No properties have inferable types
|
||||
|
||||
const obj3 = id({
|
||||
>obj3 : Symbol(obj3, Decl(reverseMappedPartiallyInferableTypes.ts, 87, 5))
|
||||
>id : Symbol(id, Decl(reverseMappedPartiallyInferableTypes.ts, 62, 1))
|
||||
|
||||
foo: {
|
||||
>foo : Symbol(foo, Decl(reverseMappedPartiallyInferableTypes.ts, 87, 17))
|
||||
|
||||
contains(k) {
|
||||
>contains : Symbol(contains, Decl(reverseMappedPartiallyInferableTypes.ts, 88, 10))
|
||||
>k : Symbol(k, Decl(reverseMappedPartiallyInferableTypes.ts, 89, 17))
|
||||
|
||||
return k.length > 0;
|
||||
>k : Symbol(k, Decl(reverseMappedPartiallyInferableTypes.ts, 89, 17))
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@ -0,0 +1,231 @@
|
||||
=== tests/cases/compiler/reverseMappedPartiallyInferableTypes.ts ===
|
||||
// Repro from #30505
|
||||
|
||||
export type Prop<T> = { (): T }
|
||||
>Prop : Prop<T>
|
||||
|
||||
export type PropType<T> = Prop<T>;
|
||||
>PropType : Prop<T>
|
||||
|
||||
export type PropDefaultValue<T> = T;
|
||||
>PropDefaultValue : T
|
||||
|
||||
|
||||
export type PropValidatorFunction<T> = (value: T) => boolean;
|
||||
>PropValidatorFunction : PropValidatorFunction<T>
|
||||
>value : T
|
||||
|
||||
export type PropValidator<T> = PropOptions<T>;
|
||||
>PropValidator : PropOptions<T>
|
||||
|
||||
|
||||
export type PropOptions<T> = {
|
||||
>PropOptions : PropOptions<T>
|
||||
|
||||
type: PropType<T>;
|
||||
>type : Prop<T>
|
||||
|
||||
value?: PropDefaultValue<T>,
|
||||
>value : T | undefined
|
||||
|
||||
required?: boolean;
|
||||
>required : boolean | undefined
|
||||
|
||||
validator?: PropValidatorFunction<T>;
|
||||
>validator : PropValidatorFunction<T> | undefined
|
||||
}
|
||||
|
||||
export type RecordPropsDefinition<T> = {
|
||||
>RecordPropsDefinition : RecordPropsDefinition<T>
|
||||
|
||||
[K in keyof T]: PropValidator<T[K]>
|
||||
}
|
||||
export type PropsDefinition<T> = RecordPropsDefinition<T>;
|
||||
>PropsDefinition : RecordPropsDefinition<T>
|
||||
|
||||
|
||||
declare function extend<T>({ props }: { props: PropsDefinition<T> }): PropsDefinition<T>;
|
||||
>extend : <T>({ props }: { props: RecordPropsDefinition<T>; }) => RecordPropsDefinition<T>
|
||||
>props : RecordPropsDefinition<T>
|
||||
>props : RecordPropsDefinition<T>
|
||||
|
||||
interface MyType {
|
||||
valid: boolean;
|
||||
>valid : boolean
|
||||
}
|
||||
|
||||
const r = extend({
|
||||
>r : RecordPropsDefinition<{ notResolved: MyType; explicit: MyType; }>
|
||||
>extend({ props: { notResolved: { type: Object as PropType<MyType>, validator: x => { return x.valid; } }, explicit: { type: Object as PropType<MyType>, validator: (x: MyType) => { return x.valid; } } }}) : RecordPropsDefinition<{ notResolved: MyType; explicit: MyType; }>
|
||||
>extend : <T>({ props }: { props: RecordPropsDefinition<T>; }) => RecordPropsDefinition<T>
|
||||
>{ props: { notResolved: { type: Object as PropType<MyType>, validator: x => { return x.valid; } }, explicit: { type: Object as PropType<MyType>, validator: (x: MyType) => { return x.valid; } } }} : { props: { notResolved: { type: Prop<MyType>; validator: (x: MyType) => boolean; }; explicit: { type: Prop<MyType>; validator: (x: MyType) => boolean; }; }; }
|
||||
|
||||
props: {
|
||||
>props : { notResolved: { type: Prop<MyType>; validator: (x: MyType) => boolean; }; explicit: { type: Prop<MyType>; validator: (x: MyType) => boolean; }; }
|
||||
>{ notResolved: { type: Object as PropType<MyType>, validator: x => { return x.valid; } }, explicit: { type: Object as PropType<MyType>, validator: (x: MyType) => { return x.valid; } } } : { notResolved: { type: Prop<MyType>; validator: (x: MyType) => boolean; }; explicit: { type: Prop<MyType>; validator: (x: MyType) => boolean; }; }
|
||||
|
||||
notResolved: {
|
||||
>notResolved : { type: Prop<MyType>; validator: (x: MyType) => boolean; }
|
||||
>{ type: Object as PropType<MyType>, validator: x => { return x.valid; } } : { type: Prop<MyType>; validator: (x: MyType) => boolean; }
|
||||
|
||||
type: Object as PropType<MyType>,
|
||||
>type : Prop<MyType>
|
||||
>Object as PropType<MyType> : Prop<MyType>
|
||||
>Object : ObjectConstructor
|
||||
|
||||
validator: x => {
|
||||
>validator : (x: MyType) => boolean
|
||||
>x => { return x.valid; } : (x: MyType) => boolean
|
||||
>x : MyType
|
||||
|
||||
return x.valid;
|
||||
>x.valid : boolean
|
||||
>x : MyType
|
||||
>valid : boolean
|
||||
}
|
||||
},
|
||||
explicit: {
|
||||
>explicit : { type: Prop<MyType>; validator: (x: MyType) => boolean; }
|
||||
>{ type: Object as PropType<MyType>, validator: (x: MyType) => { return x.valid; } } : { type: Prop<MyType>; validator: (x: MyType) => boolean; }
|
||||
|
||||
type: Object as PropType<MyType>,
|
||||
>type : Prop<MyType>
|
||||
>Object as PropType<MyType> : Prop<MyType>
|
||||
>Object : ObjectConstructor
|
||||
|
||||
validator: (x: MyType) => {
|
||||
>validator : (x: MyType) => boolean
|
||||
>(x: MyType) => { return x.valid; } : (x: MyType) => boolean
|
||||
>x : MyType
|
||||
|
||||
return x.valid;
|
||||
>x.valid : boolean
|
||||
>x : MyType
|
||||
>valid : boolean
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
r.explicit
|
||||
>r.explicit : PropOptions<MyType>
|
||||
>r : RecordPropsDefinition<{ notResolved: MyType; explicit: MyType; }>
|
||||
>explicit : PropOptions<MyType>
|
||||
|
||||
r.notResolved
|
||||
>r.notResolved : PropOptions<MyType>
|
||||
>r : RecordPropsDefinition<{ notResolved: MyType; explicit: MyType; }>
|
||||
>notResolved : PropOptions<MyType>
|
||||
|
||||
r.explicit.required
|
||||
>r.explicit.required : boolean | undefined
|
||||
>r.explicit : PropOptions<MyType>
|
||||
>r : RecordPropsDefinition<{ notResolved: MyType; explicit: MyType; }>
|
||||
>explicit : PropOptions<MyType>
|
||||
>required : boolean | undefined
|
||||
|
||||
r.notResolved.required
|
||||
>r.notResolved.required : boolean | undefined
|
||||
>r.notResolved : PropOptions<MyType>
|
||||
>r : RecordPropsDefinition<{ notResolved: MyType; explicit: MyType; }>
|
||||
>notResolved : PropOptions<MyType>
|
||||
>required : boolean | undefined
|
||||
|
||||
// Modified repro from #30505
|
||||
|
||||
type Box<T> = {
|
||||
>Box : Box<T>
|
||||
|
||||
contents?: T;
|
||||
>contents : T | undefined
|
||||
|
||||
contains?(content: T): boolean;
|
||||
>contains : ((content: T) => boolean) | undefined
|
||||
>content : T
|
||||
|
||||
};
|
||||
|
||||
type Mapped<T> = {
|
||||
>Mapped : Mapped<T>
|
||||
|
||||
[K in keyof T]: Box<T[K]>;
|
||||
}
|
||||
|
||||
declare function id<T>(arg: Mapped<T>): Mapped<T>;
|
||||
>id : <T>(arg: Mapped<T>) => Mapped<T>
|
||||
>arg : Mapped<T>
|
||||
|
||||
// All properties have inferable types
|
||||
|
||||
const obj1 = id({
|
||||
>obj1 : Mapped<{ foo: string; }>
|
||||
>id({ foo: { contents: "" }}) : Mapped<{ foo: string; }>
|
||||
>id : <T>(arg: Mapped<T>) => Mapped<T>
|
||||
>{ foo: { contents: "" }} : { foo: { contents: string; }; }
|
||||
|
||||
foo: {
|
||||
>foo : { contents: string; }
|
||||
>{ contents: "" } : { contents: string; }
|
||||
|
||||
contents: ""
|
||||
>contents : string
|
||||
>"" : ""
|
||||
}
|
||||
});
|
||||
|
||||
// Some properties have inferable types
|
||||
|
||||
const obj2 = id({
|
||||
>obj2 : Mapped<{ foo: string; }>
|
||||
>id({ foo: { contents: "", contains(k) { return k.length > 0; } }}) : Mapped<{ foo: string; }>
|
||||
>id : <T>(arg: Mapped<T>) => Mapped<T>
|
||||
>{ foo: { contents: "", contains(k) { return k.length > 0; } }} : { foo: { contents: string; contains(k: string): boolean; }; }
|
||||
|
||||
foo: {
|
||||
>foo : { contents: string; contains(k: string): boolean; }
|
||||
>{ contents: "", contains(k) { return k.length > 0; } } : { contents: string; contains(k: string): boolean; }
|
||||
|
||||
contents: "",
|
||||
>contents : string
|
||||
>"" : ""
|
||||
|
||||
contains(k) {
|
||||
>contains : (k: string) => boolean
|
||||
>k : string
|
||||
|
||||
return k.length > 0;
|
||||
>k.length > 0 : boolean
|
||||
>k.length : number
|
||||
>k : string
|
||||
>length : number
|
||||
>0 : 0
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// No properties have inferable types
|
||||
|
||||
const obj3 = id({
|
||||
>obj3 : Mapped<unknown>
|
||||
>id({ foo: { contains(k) { return k.length > 0; } }}) : Mapped<unknown>
|
||||
>id : <T>(arg: Mapped<T>) => Mapped<T>
|
||||
>{ foo: { contains(k) { return k.length > 0; } }} : { foo: { contains(k: unknown): boolean; }; }
|
||||
|
||||
foo: {
|
||||
>foo : { contains(k: unknown): boolean; }
|
||||
>{ contains(k) { return k.length > 0; } } : { contains(k: unknown): boolean; }
|
||||
|
||||
contains(k) {
|
||||
>contains : (k: unknown) => boolean
|
||||
>k : unknown
|
||||
|
||||
return k.length > 0;
|
||||
>k.length > 0 : boolean
|
||||
>k.length : any
|
||||
>k : unknown
|
||||
>length : any
|
||||
>0 : 0
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
96
tests/cases/compiler/reverseMappedPartiallyInferableTypes.ts
Normal file
96
tests/cases/compiler/reverseMappedPartiallyInferableTypes.ts
Normal file
@ -0,0 +1,96 @@
|
||||
// @strict: true
|
||||
|
||||
// Repro from #30505
|
||||
|
||||
export type Prop<T> = { (): T }
|
||||
export type PropType<T> = Prop<T>;
|
||||
export type PropDefaultValue<T> = T;
|
||||
|
||||
|
||||
export type PropValidatorFunction<T> = (value: T) => boolean;
|
||||
export type PropValidator<T> = PropOptions<T>;
|
||||
|
||||
|
||||
export type PropOptions<T> = {
|
||||
type: PropType<T>;
|
||||
|
||||
value?: PropDefaultValue<T>,
|
||||
required?: boolean;
|
||||
validator?: PropValidatorFunction<T>;
|
||||
}
|
||||
|
||||
export type RecordPropsDefinition<T> = {
|
||||
[K in keyof T]: PropValidator<T[K]>
|
||||
}
|
||||
export type PropsDefinition<T> = RecordPropsDefinition<T>;
|
||||
|
||||
|
||||
declare function extend<T>({ props }: { props: PropsDefinition<T> }): PropsDefinition<T>;
|
||||
|
||||
interface MyType {
|
||||
valid: boolean;
|
||||
}
|
||||
|
||||
const r = extend({
|
||||
props: {
|
||||
notResolved: {
|
||||
type: Object as PropType<MyType>,
|
||||
validator: x => {
|
||||
return x.valid;
|
||||
}
|
||||
},
|
||||
explicit: {
|
||||
type: Object as PropType<MyType>,
|
||||
validator: (x: MyType) => {
|
||||
return x.valid;
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
r.explicit
|
||||
r.notResolved
|
||||
r.explicit.required
|
||||
r.notResolved.required
|
||||
|
||||
// Modified repro from #30505
|
||||
|
||||
type Box<T> = {
|
||||
contents?: T;
|
||||
contains?(content: T): boolean;
|
||||
};
|
||||
|
||||
type Mapped<T> = {
|
||||
[K in keyof T]: Box<T[K]>;
|
||||
}
|
||||
|
||||
declare function id<T>(arg: Mapped<T>): Mapped<T>;
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user