Skip to content

Commit dfa8df2

Browse files
committed
fix(cdk/schematics): update JSON.parse usages to cast the result as a type
Update JSON.parse usages to be better typed rather than resulting in an any type.
1 parent 9c67b5c commit dfa8df2

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/cdk/schematics/ng-add/index.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import {COLLECTION_PATH} from '../index.spec';
44
import {createTestApp, getFileContent} from '../testing';
55
import {addPackageToPackageJson} from './package-config';
66

7+
interface PackageJson {
8+
dependencies: {[key: string]: string};
9+
}
10+
711
describe('CDK ng-add', () => {
812
let runner: SchematicTestRunner;
913
let appTree: Tree;
@@ -15,7 +19,7 @@ describe('CDK ng-add', () => {
1519

1620
it('should update the package.json', async () => {
1721
const tree = await runner.runSchematicAsync('ng-add', {}, appTree).toPromise();
18-
const packageJson = JSON.parse(getFileContent(tree, '/package.json'));
22+
const packageJson = JSON.parse(getFileContent(tree, '/package.json')) as PackageJson;
1923
const dependencies = packageJson.dependencies;
2024

2125
expect(dependencies['@angular/cdk']).toBe('~0.0.0-PLACEHOLDER');
@@ -33,7 +37,7 @@ describe('CDK ng-add', () => {
3337
addPackageToPackageJson(appTree, '@angular/cdk', '^9.0.0');
3438

3539
const tree = await runner.runSchematicAsync('ng-add', {}, appTree).toPromise();
36-
const packageJson = JSON.parse(getFileContent(tree, '/package.json'));
40+
const packageJson = JSON.parse(getFileContent(tree, '/package.json')) as PackageJson;
3741
const dependencies = packageJson.dependencies;
3842

3943
expect(dependencies['@angular/cdk']).toBe('^9.0.0');

src/cdk/schematics/ng-add/package-config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
import {Tree} from '@angular-devkit/schematics';
1010

11+
interface PackageJson {
12+
dependencies: {[key: string]: string};
13+
}
14+
1115
/**
1216
* Sorts the keys of the given object.
1317
* @returns A new object instance with sorted keys
@@ -21,7 +25,7 @@ export function addPackageToPackageJson(host: Tree, pkg: string, version: string
2125

2226
if (host.exists('package.json')) {
2327
const sourceText = host.read('package.json')!.toString('utf-8');
24-
const json = JSON.parse(sourceText);
28+
const json = JSON.parse(sourceText) as PackageJson;
2529

2630
if (!json.dependencies) {
2731
json.dependencies = {};
@@ -44,7 +48,7 @@ export function getPackageVersionFromPackageJson(tree: Tree, name: string): stri
4448
return null;
4549
}
4650

47-
const packageJson = JSON.parse(tree.read('package.json')!.toString('utf8'));
51+
const packageJson = JSON.parse(tree.read('package.json')!.toString('utf8')) as PackageJson;
4852

4953
if (packageJson.dependencies && packageJson.dependencies[name]) {
5054
return packageJson.dependencies[name];

0 commit comments

Comments
 (0)