Skip to content

Commit

Permalink
Add TS types (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed Feb 1, 2024
1 parent d1070c6 commit 1d9569f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/workflows/linting.yml
Expand Up @@ -36,3 +36,6 @@ jobs:

- name: Run lint
run: npm run lint

- name: Run TS Type Tests
run: npm run test:types
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -82,3 +82,5 @@ generates this output:
}
}
```

Special thanks to Jorge Yero Salazar for providing TS typings!
18 changes: 18 additions & 0 deletions index.d.ts
@@ -0,0 +1,18 @@
/**
* Types created by Jorge Yero Salazar (jyeros on GitHub)
*/

export type WithFieldMask<T> = { [K in keyof T]?: WithFieldMask<T[K]> | undefined };
/**
* Creates a new object that copies fields present in field mask from specified source object
* @param sourceObject - object to apply field mask to
* @param fieldMask
* @returns new object created by applying field mask on source object or original entity if source is not an object
*/
export function applyFieldMask<T>(sourceObject: T, fieldMask: readonly string[]): WithFieldMask<T>;
/**
* Generates field mask that includes all non-function own properties on specified object
* @param object - object to generate field mask from
* @returns - generated field mask
*/
export function generateFieldMask(object: unknown): string[];
9 changes: 9 additions & 0 deletions index.test-d.ts
@@ -0,0 +1,9 @@
import { applyFieldMask, generateFieldMask, WithFieldMask } from "./";
import { expectType } from "tsd";

const obj: Record<string, string> = {
foo: "bar",
};

expectType<WithFieldMask<Record<string, string>>>(applyFieldMask(obj, ["foo"]))
expectType<string[]>(generateFieldMask(obj))
8 changes: 7 additions & 1 deletion package.json
Expand Up @@ -9,10 +9,14 @@
"email": "kibertoad@gmail.com"
}
],
"main": "index.js",
"types": "index.d.ts",
"type": "commonjs",
"scripts": {
"coveralls": "nyc report --reporter=lcov",
"test": "mocha ./test",
"test:coverage": "nyc npm test",
"test:types": "tsd",
"lint": "npx @biomejs/biome@1.5.3 lint index.js lib test biome.json",
"lint:fix": "npx @biomejs/biome@1.5.3 check --apply index.js lib test biome.json",
"prettier": "prettier --write \"{lib,test}/**/*.{js,ts}\""
Expand All @@ -38,7 +42,8 @@
"devDependencies": {
"chai": "^4.4.1",
"mocha": "^7.2.0",
"nyc": "^15.1.0"
"nyc": "^15.1.0",
"tsd": "^0.30.4"
},
"nyc": {
"description": "test coverage",
Expand All @@ -59,6 +64,7 @@
"files": [
"lib/*",
"index.js",
"index.d.ts",
"LICENSE",
"README.md"
]
Expand Down

0 comments on commit 1d9569f

Please sign in to comment.