人民是土壤,它含有要切事物发展所必需的生命汁液;而个人则是这土壤上的花朵
与果实。——别林斯基

题目:

1
2
3
declare module 'stats' {
export function getMaxIndex(input: unknown, comparator: unknown): unknown;
}

报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
index.ts(3,5): error TS2305: Module '"stats"' has no exported member 'getMaxElement'.
index.ts(4,5): error TS2724: Module '"stats"' has no exported member 'getMinIndex'. Did you mean 'getMaxIndex'?
index.ts(5,5): error TS2305: Module '"stats"' has no exported member 'getMinElement'.
index.ts(6,5): error TS2305: Module '"stats"' has no exported member 'getMedianIndex'.
index.ts(7,5): error TS2305: Module '"stats"' has no exported member 'getMedianElement'.
index.ts(8,5): error TS2305: Module '"stats"' has no exported member 'getAverageValue'.
index.ts(126,37): error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'number'.
index.ts(149,37): error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'number'.
test.ts(13,5): error TS2344: Type 'false' does not satisfy the constraint 'true'.
test.ts(20,5): error TS2344: Type 'false' does not satisfy the constraint 'true'.
test.ts(27,5): error TS2344: Type 'false' does not satisfy the constraint 'true'.
test.ts(34,5): error TS2344: Type 'false' does not satisfy the constraint 'true'.
test.ts(41,5): error TS2344: Type 'false' does not satisfy the constraint 'true'.
test.ts(48,5): error TS2344: Type 'false' does not satisfy the constraint 'true'.
test.ts(55,5): error TS2344: Type 'false' does not satisfy the constraint 'true'.

答案:

1
2
3
4
5
6
7
8
9
declare module 'stats' {
export function getMaxIndex<T>(input: T[], comparator: (a:T, b:T) => number): number;
export function getMaxElement<T>(input: T[], comparator: (a:T, b:T) => number): T | null;
export function getMinIndex<T>(input: T[], comparator: (a:T, b:T) => number): number;
export function getMinElement<T>(input: T[], comparator: (a:T, b:T) => number): T | null;
export function getMedianIndex<T>(input: T[], comparator: (a:T, b:T) => number): number;
export function getMedianElement<T>(input: T[], comparator: (a:T, b:T) => number): T | null;
export function getAverageValue<T>(input: T[], getValue: (item: T) => number): number | null;
}