Remove unnecessary type assertions#62969
Conversation
|
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
1 similar comment
|
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
There was a problem hiding this comment.
Pull request overview
This pull request removes unnecessary type assertions across the TypeScript codebase that were identified by improvements to the @typescript-eslint/no-unnecessary-type-assertion lint rule. The changes are purely cosmetic, removing as Type assertions where TypeScript's type inference already provides the correct type information.
Key changes:
- Removal of redundant type assertions in type guard contexts (e.g., after
flags & TypeFlags.TypeParameterchecks) - Elimination of unnecessary casts where function signatures already accept the broader type
- Cleanup of assertions where factory methods return the correct specific type
Reviewed changes
Copilot reviewed 25 out of 25 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/testRunner/unittests/tsserver/inlayHints.ts | Removed unnecessary assertion on preferences object |
| src/testRunner/projectsRunner.ts | Removed redundant string assertion on baseline content |
| src/services/symbolDisplay.ts | Removed type assertion where type parameter check provides correct type |
| src/services/stringCompletions.ts | Removed host interface assertions where type is already compatible |
| src/services/services.ts | Removed SyntaxKind assertion on Number() result |
| src/services/getEditsForFileRename.ts | Removed ModuleResolutionHost assertion |
| src/services/findAllReferences.ts | Removed assertion on findAncestor result with type guard |
| src/services/completions.ts | Removed ObjectTypeDeclaration assertion on findAncestor |
| src/services/codefixes/generateAccessors.ts | Removed assertion on declaration.name.text |
| src/services/codefixes/fixUnreferenceableDecoratorMetadata.ts | Removed assertion on or() predicate result |
| src/services/classifier.ts | Removed ClassificationType assertion on array access |
| src/server/utilitiesPublic.ts | Removed NormalizedPath[] assertion on concat |
| src/server/session.ts | Removed string assertion on symlinkedPath |
| src/server/project.ts | Removed SortedReadonlyArray assertion on emptyArray |
| src/server/editorServices.ts | Removed ProgramUpdateLevel assertion on Math.max result |
| src/compiler/utilitiesPublic.ts | Removed Identifier assertion where type is already known |
| src/compiler/utilities.ts | Removed NonNullable assertion on generic parameter |
| src/compiler/tsbuildPublic.ts | Removed CompilerHost & ReadBuildProgramHost assertion |
| src/compiler/transformers/ts.ts | Removed ModuleDeclaration assertion after type check |
| src/compiler/transformers/es2018.ts | Removed NodeCheckFlags assertion on literal 0 |
| src/compiler/symbolWalker.ts | Removed TypeParameter assertion after flags check |
| src/compiler/parser.ts | Removed callback type assertions for forEachChild |
| src/compiler/emitter.ts | Removed DotToken, Modifier[], and Decorator[] assertions |
| src/compiler/checker.ts | Removed 50+ TypeParameter, UnionType, and other type assertions after flag checks |
| src/compiler/binder.ts | Removed Identifier assertions where function accepts broader Node type |
|
Those are good improvements but I would doubt we'd merge this given the current TS codebase is going to get overwritten in the somewhat near future: #62963 |
Thanks and no worries! You can close the PR if you want. This was more of a proof of concept for my lint changes, but I wanted to propose them anyway. |
While testing my changes to the @typescript-eslint/no-unnecessary-type-assertion lint rule on this repo, I found these unnecessary type assertions that can be removed without impacting runtime behaviour or triggering TS errors.
typescript-eslint/typescript-eslint#11789