Skip to content

Commit

Permalink
Types testing with tsd has been added
Browse files Browse the repository at this point in the history
  • Loading branch information
bsalex committed Nov 29, 2020
1 parent 670897d commit e785d89
Show file tree
Hide file tree
Showing 5 changed files with 744 additions and 20 deletions.
46 changes: 46 additions & 0 deletions dist/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {expectType, expectDeprecated, expectError} from 'tsd';
import {typedPath, TypedPathKey} from '.';

const sym = Symbol('SomeSymbol');


type TestType = {
a: {
testFunc: () => {result: string};
b: {
arrayOfArrays: string[][];
c: number;
f: {test: string; blah: {path?: string}; arr: string[]}[];
};
[sym]: {
g: string;
};
};
};

const testAdditionalHandlers = {
$abs: (path: TypedPathKey[]) =>
typedPath<TestType, typeof testAdditionalHandlers>(testAdditionalHandlers, ['', ...path]),
$url: (path: TypedPathKey[]) => path.join('/'),
$length: (path: TypedPathKey[]) => path.length
};

// Default handlers
expectType<string>(typedPath<TestType>().a.b.c.$path);
expectType<string[]>(typedPath<TestType>().a.b.c.$raw);
expectType<string>(typedPath<TestType>().a.b.c.toString());
expectType<TypedPathKey[]>(typedPath<TestType>().a.b.c.$rawPath);
// with array index
expectType<TypedPathKey[]>(typedPath<TestType>().a.b.f[5].$rawPath);
expectDeprecated(typedPath<TestType>().a.b.c.$raw);
// Default handlers


// Type-safe path errors
expectError(typedPath<TestType>().a.W.c.$rawPath);
// Type-safe path errors


// Types for additional handlers
expectType<number>(typedPath<TestType, typeof testAdditionalHandlers>(testAdditionalHandlers).a.b.c.$length);
// Types for additional handlers
20 changes: 13 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "typed-path",
"version": "2.2.1",
"description": "Type safe object string paths for typescript.",
"main": "index.js",
"main": "dist/index.js",
"repository": {
"type": "git",
"url": "https://github.com/bsalex/typed-path.git"
Expand All @@ -24,25 +24,31 @@
"files": [
"LICENSE",
"index.ts",
"index.js",
"index.js.map",
"index.d.ts"
"dist/index.js",
"dist/index.js.map",
"dist/index.d.ts"
],
"types": "dist/index.d.ts",
"scripts": {
"test": "jest index.spec.ts",
"test": "yarn tsdlint && jest index.spec.ts",
"lint": "tslint 'src/**/*.ts' 'src/**/*.tsx'",
"tsdlint": "yarn build && tsd",
"format": "prettier --parser typescript --write index.ts index.spec.ts",
"prepush": "yarn lint && yarn build && git diff --exit-code",
"prepush": "yarn prepare",
"prepare": "yarn test && yarn build",
"build": "tsc"
"build": "tsc -p tsconfig.dist.json"
},
"devDependencies": {
"husky": "^0.13.1",
"jest": "^26.6.3",
"prettier": "2.2.0",
"ts-jest": "^26.4.4",
"ts-node": "^9.0.0",
"tsd": "^0.13.1",
"tslint": "^4.4.2",
"typescript": "^4.0.5"
},
"tsd": {
"directory": "dist"
}
}
16 changes: 16 additions & 0 deletions tsconfig.dist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"noImplicitAny": true,
"sourceMap": true,
"declaration": true,
"lib": ["es6", "es7", "dom"],
"strict": true,
"outDir": "dist"
},
"compileOnSave": false,
"include": [
"index.ts"
]
}
8 changes: 5 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
"sourceMap": true,
"declaration": true,
"lib": ["es6", "es7", "dom"],
"strict": true
"strict": true,
"noEmit": true
},
"compileOnSave": false,
"files": [
"include": [
"index.ts",
"index.spec.ts"
"index.spec.ts",
"dist/index.test-d.ts"
]
}
Loading

0 comments on commit e785d89

Please sign in to comment.