Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add TypeScript typings #46

Merged
merged 2 commits into from
Aug 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.ts
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ language: node_js
node_js:
- v6
script:
- npm run lint && npm test -- --coverage
- npm run lint
- npm test
- npm run type-check
cache:
- yarn
after_success:
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./src";
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"repository": "diegohaz/styled-tools",
"main": "dist/cjs/index.js",
"module": "dist/es/index.js",
"types": "types/index.d.ts",
"author": {
"name": "Diego Haz",
"email": "[email protected]",
Expand All @@ -15,13 +16,15 @@
"node": ">=6"
},
"files": [
"dist"
"dist",
"types"
],
"scripts": {
"test": "jest",
"coverage": "npm test -- --coverage",
"postcoverage": "opn coverage/lcov-report/index.html",
"lint": "eslint src test benchmark",
"type-check": "tsc",
"flow": "flow check",
"flow-typed": "flow-typed install",
"benchmark": "cross-env NODE_ENV=test babel-node benchmark",
Expand Down Expand Up @@ -70,6 +73,7 @@
"rimraf": "^2.6.2",
"standard-changelog": "^2.0.0",
"styled-components": "^3.3.3",
"styled-tools": "^1.0.0"
"styled-tools": "^1.0.0",
"typescript": "^3.0.1"
}
}
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"noEmit": true,
"strict": true,
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true
}
}
28 changes: 28 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
type Dictionary<T = any> = { [key: string]: T };
type PropsFn<ReturnType = any> = (props?: Dictionary) => ReturnType;
type Needle = string | PropsFn;

export function ifProp<Pass = string, Fail = string>(
test: Needle | Needle[] | Dictionary,
pass?: Pass,
fail?: Fail
): PropsFn<Pass | Fail>;

export function ifNotProp<Pass = string, Fail = string>(
test: Needle | Needle[] | Dictionary,
pass?: Pass,
fail?: Fail
): PropsFn<Pass | Fail>;

export function prop(path: string, defaultValue?: any): PropsFn;

export function switchProp<T = any, DefaultCase = undefined>(
needle: Needle,
cases: Dictionary<T>,
defaultCase?: DefaultCase
): PropsFn<T | DefaultCase>;

export function withProp<T = any>(
needle: Needle | Needle[],
fn: (...props: any[]) => T
): PropsFn<T>
20 changes: 20 additions & 0 deletions types/tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ifProp, ifNotProp, prop, switchProp, withProp } from "..";

ifProp("a", true)({ a: true });
ifProp("a", true, false)({ a: true });
ifProp("a", true, false)({ a: true });

ifNotProp("a", true)({ a: true });
ifNotProp("a", true, false)({ a: true });
ifNotProp("a", true, false)({ a: true });

prop("a")({ a: true });
prop("a")({ b: true });
prop("a", "b")({ b: true });

switchProp("a", { a: true, b: "c" })({ a: true });
switchProp("a", { a: true, b: "c" }, 1)({ a: true });
switchProp("a", { a: true, b: "c" }, 1)({ a: "a" });

withProp("a", (a: boolean) => a)({ a: false });
withProp(["a", "b"], (a: number, b: string) => b)({ a: false });
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6563,6 +6563,10 @@ typedarray@^0.0.6, typedarray@~0.0.5:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

typescript@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.0.1.tgz#43738f29585d3a87575520a4b93ab6026ef11fdb"

ua-parser-js@^0.7.18:
version "0.7.18"
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.18.tgz#a7bfd92f56edfb117083b69e31d2aa8882d4b1ed"
Expand Down