Skip to content

Commit f73308b

Browse files
committed
Add tests
1 parent 8891d4f commit f73308b

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// @strict: true
2+
3+
// Repro from #30505
4+
5+
export type Prop<T> = { (): T }
6+
export type PropType<T> = Prop<T>;
7+
export type PropDefaultValue<T> = T;
8+
9+
10+
export type PropValidatorFunction<T> = (value: T) => boolean;
11+
export type PropValidator<T> = PropOptions<T>;
12+
13+
14+
export type PropOptions<T> = {
15+
type: PropType<T>;
16+
17+
value?: PropDefaultValue<T>,
18+
required?: boolean;
19+
validator?: PropValidatorFunction<T>;
20+
}
21+
22+
export type RecordPropsDefinition<T> = {
23+
[K in keyof T]: PropValidator<T[K]>
24+
}
25+
export type PropsDefinition<T> = RecordPropsDefinition<T>;
26+
27+
28+
declare function extend<T>({ props }: { props: PropsDefinition<T> }): PropsDefinition<T>;
29+
30+
interface MyType {
31+
valid: boolean;
32+
}
33+
34+
const r = extend({
35+
props: {
36+
notResolved: {
37+
type: Object as PropType<MyType>,
38+
validator: x => {
39+
return x.valid;
40+
}
41+
},
42+
explicit: {
43+
type: Object as PropType<MyType>,
44+
validator: (x: MyType) => {
45+
return x.valid;
46+
}
47+
}
48+
}
49+
})
50+
51+
r.explicit
52+
r.notResolved
53+
r.explicit.required
54+
r.notResolved.required
55+
56+
// Modified repro from #30505
57+
58+
type Box<T> = {
59+
contents?: T;
60+
contains?(content: T): boolean;
61+
};
62+
63+
type Mapped<T> = {
64+
[K in keyof T]: Box<T[K]>;
65+
}
66+
67+
declare function id<T>(arg: Mapped<T>): Mapped<T>;
68+
69+
// All properties have inferable types
70+
71+
const obj1 = id({
72+
foo: {
73+
contents: ""
74+
}
75+
});
76+
77+
// Some properties have inferable types
78+
79+
const obj2 = id({
80+
foo: {
81+
contents: "",
82+
contains(k) {
83+
return k.length > 0;
84+
}
85+
}
86+
});
87+
88+
// No properties have inferable types
89+
90+
const obj3 = id({
91+
foo: {
92+
contains(k) {
93+
return k.length > 0;
94+
}
95+
}
96+
});

0 commit comments

Comments
 (0)