-
Notifications
You must be signed in to change notification settings - Fork 23
/
test.ts.lint
68 lines (60 loc) · 5.16 KB
/
test.ts.lint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
declare function get(): void;
declare function get<T>(): T;
~ [error no-misused-generics: TypeParameter 'T' cannot be inferred from any parameter.]
declare function get<T extends object>(): T;
~~~~~~~~~~~~~~~~ [error no-misused-generics: TypeParameter 'T' cannot be inferred from any parameter.]
declare function get<T, U = T>(param: U): U;
~ [error no-misused-generics: TypeParameter 'T' cannot be inferred from any parameter.]
declare function get<T, U extends T = T>(param: T): U;
~~~~~~~~~~~~~~~ [error no-misused-generics: TypeParameter 'U' cannot be inferred from any parameter.]
declare function get<T>(param: T[]): T;
declare function get<T extends string, U>(param: Record<T, U>): boolean;
~~~~~~~~~~~~~~~~ [error no-misused-generics: TypeParameter 'T' is not used to enforce a constraint between types and can be replaced with 'string'.]
~ [error no-misused-generics: TypeParameter 'U' is not used to enforce a constraint between types and can be replaced with 'any'.]
declare function get<T>(param: <T, U>(param: T) => U): T
~ [error no-misused-generics: TypeParameter 'T' cannot be inferred from any parameter.]
~ [error no-misused-generics: TypeParameter 'T' is not used to enforce a constraint between types and can be replaced with 'any'.]
~ [error no-misused-generics: TypeParameter 'U' cannot be inferred from any parameter.]
function fn<T>(param: string) {
~ [error no-misused-generics: TypeParameter 'T' cannot be inferred from any parameter.]
let v: T = null!;
return v;
}
declare class C<V> {
method<T, U>(param: T): U;
~ [error no-misused-generics: TypeParameter 'T' is not used to enforce a constraint between types and can be replaced with 'any'.]
~ [error no-misused-generics: TypeParameter 'U' cannot be inferred from any parameter.]
prop: <T>() => T;
~ [error no-misused-generics: TypeParameter 'T' cannot be inferred from any parameter.]
}
<T>(param): T => null!;
~ [error no-misused-generics: TypeParameter 'T' cannot be inferred from any parameter.]
get<string>();
declare function take<T>(param: T): void; // T not used as constraint -> could just be `any`
~ [error no-misused-generics: TypeParameter 'T' is not used to enforce a constraint between types and can be replaced with 'any'.]
declare function take<T extends object>(param: T): void; // could just use `object`
~~~~~~~~~~~~~~~~ [error no-misused-generics: TypeParameter 'T' is not used to enforce a constraint between types and can be replaced with 'object'.]
declare function take<T, U = T>(param1: T, param2: U): void; // no constraint
~ [error no-misused-generics: TypeParameter 'T' is not used to enforce a constraint between types and can be replaced with 'any'.]
~~~~~ [error no-misused-generics: TypeParameter 'U' is not used to enforce a constraint between types and can be replaced with 'any'.]
declare function take<T, U extends T>(param: T): U; // U is only used in the return type
~~~~~~~~~~~ [error no-misused-generics: TypeParameter 'U' cannot be inferred from any parameter.]
declare function take<T, U extends T>(param: U): U; // T cannot be inferred
~ [error no-misused-generics: TypeParameter 'T' cannot be inferred from any parameter.]
declare function identity<T>(param: T): T; // this is valid as it constrains the return type to the parameter type
declare function compare<T>(param1: T, param2: T): boolean; // this is valid because it enforces comparable types for both parameters
declare function compare<T, U extends T>(param1: T, param2: U): boolean; // this is also valid because T constrains U
// type parameters in implementations are always necessary, because they enforce type safety in the function body
function doStuff<K, V>(map: Map<K, V>, key: K) {
let v = map.get(key);
v = 1; // this error disappears if V is replaced with `any`
map.set(key, v);
return v; // signature has implicit return type `V`, but we cannot know that without type information
}
declare class Foo {
prop: string;
getProp<T>(this: Record<'prop', T>): T;
compare<T>(this: Record<'prop', T>, other: Record<'prop', T>): number;
foo<T>(this: T): void;
~ [error no-misused-generics: TypeParameter 'T' is not used to enforce a constraint between types and can be replaced with 'any'.]
}