-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
no-useless-assertion: add test with conditional types
Fixes: #116
- Loading branch information
Showing
10 changed files
with
123 additions
and
6 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
baselines/packages/mimir/test/no-useless-assertion/conditional-types/conditional-types.ts
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,34 @@ | ||
export {}; | ||
|
||
type Nullable<T> = T extends null | undefined ? T : T | null | undefined; | ||
type NonNullable<T> = T extends null | undefined ? never : T; | ||
|
||
declare function nullable<T>(param: T): Nullable<T>; | ||
declare function nonNullable<T>(param: T): NonNullable<T>; | ||
|
||
declare function takeString(param: string): void; | ||
declare function takeNullable(param: Nullable<string>): void; | ||
|
||
declare let v: Nullable<string>; | ||
declare let s: NonNullable<typeof v>; | ||
|
||
takeString(v!); | ||
takeNullable(v); | ||
takeString(s); | ||
takeNullable(s); | ||
takeNullable(nullable(s)); | ||
takeString(nonNullable(v)); | ||
|
||
nullable(s)!; | ||
nonNullable(s); | ||
nullable(null)!; | ||
nonNullable(null); | ||
|
||
nullable(s) as string; | ||
nullable(s); | ||
nonNullable(s); | ||
|
||
v as NonNullable<typeof s>; | ||
s as Nullable<string>; | ||
s; | ||
(Boolean() ? s : null) as NonNullable<string>; |
44 changes: 44 additions & 0 deletions
44
...ines/packages/mimir/test/no-useless-assertion/conditional-types/conditional-types.ts.lint
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,44 @@ | ||
export {}; | ||
|
||
type Nullable<T> = T extends null | undefined ? T : T | null | undefined; | ||
type NonNullable<T> = T extends null | undefined ? never : T; | ||
|
||
declare function nullable<T>(param: T): Nullable<T>; | ||
declare function nonNullable<T>(param: T): NonNullable<T>; | ||
|
||
declare function takeString(param: string): void; | ||
declare function takeNullable(param: Nullable<string>): void; | ||
|
||
declare let v: Nullable<string>; | ||
declare let s: NonNullable<typeof v>; | ||
|
||
takeString(v!); | ||
takeNullable(v!); | ||
~ [error no-useless-assertion: This assertion is unnecessary as the receiver accepts 'null' and 'undefined' values.] | ||
takeString(s!); | ||
~ [error no-useless-assertion: This assertion is unnecesary as it doesn't change the type of the expression.] | ||
takeNullable(s!); | ||
~ [error no-useless-assertion: This assertion is unnecesary as it doesn't change the type of the expression.] | ||
takeNullable(nullable(s)!); | ||
~ [error no-useless-assertion: This assertion is unnecessary as the receiver accepts 'null' and 'undefined' values.] | ||
takeString(nonNullable(v)!); | ||
~ [error no-useless-assertion: This assertion is unnecesary as it doesn't change the type of the expression.] | ||
|
||
nullable(s)!; | ||
nonNullable(s)!; | ||
~ [error no-useless-assertion: This assertion is unnecesary as it doesn't change the type of the expression.] | ||
nullable(null)!; | ||
nonNullable(null)!; | ||
~ [error no-useless-assertion: This assertion is unnecesary as it doesn't change the type of the expression.] | ||
|
||
nullable(s) as string; | ||
nullable(s) as string | null | undefined; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [error no-useless-assertion: This assertion is unnecesary as it doesn't change the type of the expression.] | ||
nonNullable(s) as string; | ||
~~~~~~~~~ [error no-useless-assertion: This assertion is unnecesary as it doesn't change the type of the expression.] | ||
|
||
v as NonNullable<typeof s>; | ||
s as Nullable<string>; | ||
s as NonNullable<string>; | ||
~~~~~~~~~~~~~~~~~~~~~~ [error no-useless-assertion: This assertion is unnecesary as it doesn't change the type of the expression.] | ||
(Boolean() ? s : null) as NonNullable<string>; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"project": "tsconfig.json", | ||
"exclude": ["global2.ts", "definite-assignment.ts"], | ||
"files": ["global.ts", "non-null.ts", "type-assertion.ts"], | ||
"typescriptVersion": "~2.7.0" | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/mimir/test/no-useless-assertion/conditional-types.test.json
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,5 @@ | ||
{ | ||
"project": "tsconfig.json", | ||
"files": ["conditional-types.ts"], | ||
"typescriptVersion": ">= 2.8.0" | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/mimir/test/no-useless-assertion/conditional-types.ts
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,34 @@ | ||
export {}; | ||
|
||
type Nullable<T> = T extends null | undefined ? T : T | null | undefined; | ||
type NonNullable<T> = T extends null | undefined ? never : T; | ||
|
||
declare function nullable<T>(param: T): Nullable<T>; | ||
declare function nonNullable<T>(param: T): NonNullable<T>; | ||
|
||
declare function takeString(param: string): void; | ||
declare function takeNullable(param: Nullable<string>): void; | ||
|
||
declare let v: Nullable<string>; | ||
declare let s: NonNullable<typeof v>; | ||
|
||
takeString(v!); | ||
takeNullable(v!); | ||
takeString(s!); | ||
takeNullable(s!); | ||
takeNullable(nullable(s)!); | ||
takeString(nonNullable(v)!); | ||
|
||
nullable(s)!; | ||
nonNullable(s)!; | ||
nullable(null)!; | ||
nonNullable(null)!; | ||
|
||
nullable(s) as string; | ||
nullable(s) as string | null | undefined; | ||
nonNullable(s) as string; | ||
|
||
v as NonNullable<typeof s>; | ||
s as Nullable<string>; | ||
s as NonNullable<string>; | ||
(Boolean() ? s : null) as NonNullable<string>; |
2 changes: 1 addition & 1 deletion
2
packages/mimir/test/no-useless-assertion/post270-loose.test.json
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"project": "tsconfig-loose.json", | ||
"exclude": ["global2.ts", "definite-assignment.ts"], | ||
"files": ["global.ts", "non-null.ts", "type-assertion.ts"], | ||
"typescriptVersion": ">= 2.7.0" | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/mimir/test/no-useless-assertion/post270-strict.test.json
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"project": "tsconfig.json", | ||
"exclude": ["global2.ts", "definite-assignment.ts"], | ||
"files": ["global.ts", "non-null.ts", "type-assertion.ts"], | ||
"typescriptVersion": ">= 2.8.0" | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/mimir/test/no-useless-assertion/pre270-loose.test.json
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"project": "tsconfig-loose.json", | ||
"exclude": ["global2.ts", "definite-assignment.ts"], | ||
"files": ["global.ts", "non-null.ts", "type-assertion.ts"], | ||
"typescriptVersion": "< 2.7.0" | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/mimir/test/no-useless-assertion/pre270-strict.test.json
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"project": "tsconfig.json", | ||
"exclude": ["global2.ts", "definite-assignment.ts"], | ||
"files": ["global.ts", "non-null.ts", "type-assertion.ts"], | ||
"typescriptVersion": "< 2.7.0" | ||
} |