-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31221 from microsoft/improveReverseMappedTypes
Improve reverse mapped types
- Loading branch information
Showing
10 changed files
with
873 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
tests/baselines/reference/reverseMappedPartiallyInferableTypes.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'. | ||
} | ||
} | ||
}); | ||
|
144 changes: 144 additions & 0 deletions
144
tests/baselines/reference/reverseMappedPartiallyInferableTypes.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
}); |
Oops, something went wrong.