-
Notifications
You must be signed in to change notification settings - Fork 142
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
Improve typings #45
Merged
Merged
Improve typings #45
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5eb7a9c
Add type guard, generic, and test
unional 9d1c497
Improve generic naming
unional ce57e63
Add ErrorFSA alias
unional 644622e
Write test correctly to avoid confusion
unional 80a71fa
Add `someField` validation
unional 2a6b80a
Add somefield test
unional 8c64c1b
Add semi-colon, move tsc to posttest
unional ee78552
Add 3 more semi-colon
unional c9fa687
Remove optional designation.
unional 5216925
Fix more linting issue
unional 3df9bea
Merge remote-tracking branch 'acdlite/master'
unional b9d81be
Adding eslint for typescript. Update yarn.lock
unional 73529c4
Change typescript-eslint-parser to devDeps
unional 4157c98
Move eslint-plugin-typescirpt to devDeps also.
unional File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { FluxStandardAction, isError, isFSA } from '../src'; | ||
|
||
interface CustomPayload { | ||
a: number; | ||
} | ||
|
||
interface CustomMetadata { | ||
b: string; | ||
} | ||
|
||
interface MyError extends Error { | ||
someField: string; | ||
} | ||
|
||
function createCustomAction(payload: CustomPayload) { | ||
return { | ||
type: 'custom', | ||
payload | ||
}; | ||
} | ||
|
||
function isCustomAction(action: FluxStandardAction<any, any>): action is FluxStandardAction<CustomPayload, any> { | ||
return isFSA(action) && action.type === 'custom'; | ||
} | ||
|
||
function isCustomAction2(action: FluxStandardAction<any, any>): action is FluxStandardAction<CustomPayload, CustomMetadata> { | ||
return isFSA(action) && action.type === 'custom2'; | ||
} | ||
|
||
function isCustomAction3(action: any): action is FluxStandardAction<void, string> { | ||
return isFSA(action) && action.type === 'custom3'; | ||
} | ||
|
||
function isCustomAction4(action: any): action is FluxStandardAction<{ message: string }, void> { | ||
return true; | ||
} | ||
|
||
let action2 = {}; | ||
if (isCustomAction4(action2)) { | ||
// type guard infers payload will not be undefined | ||
console.log(action2.payload.message); | ||
} | ||
|
||
function reducer(state, action) { | ||
if (isFSA<CustomPayload, void>(action)) { | ||
let a: number = action.payload.a; | ||
} | ||
else if (isFSA<CustomPayload, CustomMetadata>(action)) { | ||
let a: number = action.payload.a; | ||
let b: string = action.meta.b; | ||
} | ||
else if (isFSA<void, string>(action)) { | ||
let meta: string = action.meta; | ||
} | ||
else if (isError(action)) { | ||
let iserr: true = action.error; // iserr === true | ||
let err: Error = action.payload; | ||
} | ||
else if (isError<MyError, void>(action)) { | ||
let err: MyError = action.payload; | ||
let someFieldValue: string = err.someField; | ||
} | ||
} | ||
|
||
function reducer2(state, action) { | ||
if (isCustomAction(action)) { | ||
let a: number = action.payload.a; | ||
} | ||
else if (isCustomAction2(action)) { | ||
let a: number = action.payload.a; | ||
let b: string = action.meta.b; | ||
} | ||
else if (isCustomAction3(action)) { | ||
let meta: string = action.meta; | ||
} | ||
} | ||
|
||
let action = createCustomAction({ a: 123 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"noEmit": true, | ||
"strictNullChecks": true, | ||
"target": "es5" | ||
}, | ||
"files": [ | ||
"test/typings-test.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1258,6 +1258,12 @@ eslint-plugin-import@^2.2.0: | |
minimatch "^3.0.3" | ||
pkg-up "^1.0.0" | ||
|
||
eslint-plugin-typescript@^0.1.0: | ||
version "0.1.0" | ||
resolved "https://registry.yarnpkg.com/eslint-plugin-typescript/-/eslint-plugin-typescript-0.1.0.tgz#6d82b7960f3ff21a499e8b157b304453be5a5700" | ||
dependencies: | ||
requireindex "~1.1.0" | ||
|
||
eslint@^3.10.2: | ||
version "3.13.0" | ||
resolved "https://registry.yarnpkg.com/eslint/-/eslint-3.13.0.tgz#636925fd163c9babe2e8be7ae43caf518d469577" | ||
|
@@ -2079,6 +2085,16 @@ lodash.pickby@^4.6.0: | |
version "4.6.0" | ||
resolved "https://registry.yarnpkg.com/lodash.pickby/-/lodash.pickby-4.6.0.tgz#7dea21d8c18d7703a27c704c15d3b84a67e33aff" | ||
|
||
lodash.tostring@^4.0.0: | ||
version "4.1.4" | ||
resolved "https://registry.yarnpkg.com/lodash.tostring/-/lodash.tostring-4.1.4.tgz#560c27d1f8eadde03c2cce198fef5c031d8298fb" | ||
|
||
[email protected]: | ||
version "4.0.0" | ||
resolved "https://registry.yarnpkg.com/lodash.unescape/-/lodash.unescape-4.0.0.tgz#36debfc492b81478471ef974cd3783e202eb6cef" | ||
dependencies: | ||
lodash.tostring "^4.0.0" | ||
|
||
lodash@^4.0.0, lodash@^4.2.0, lodash@^4.3.0: | ||
version "4.17.4" | ||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae" | ||
|
@@ -2674,6 +2690,10 @@ require-uncached@^1.0.2: | |
caller-path "^0.1.0" | ||
resolve-from "^1.0.0" | ||
|
||
requireindex@~1.1.0: | ||
version "1.1.0" | ||
resolved "https://registry.yarnpkg.com/requireindex/-/requireindex-1.1.0.tgz#e5404b81557ef75db6e49c5a72004893fe03e162" | ||
|
||
resolve-from@^1.0.0: | ||
version "1.0.1" | ||
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-1.0.1.tgz#26cbfe935d1aeeeabb29bc3fe5aeb01e93d44226" | ||
|
@@ -2972,6 +2992,17 @@ typedarray@^0.0.6: | |
version "0.0.6" | ||
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" | ||
|
||
typescript-eslint-parser@^1.0.2: | ||
version "1.0.2" | ||
resolved "https://registry.yarnpkg.com/typescript-eslint-parser/-/typescript-eslint-parser-1.0.2.tgz#fd2abacf2ee3d9382ab3e449c8762b6beae4d0d7" | ||
dependencies: | ||
lodash.unescape "4.0.0" | ||
object-assign "^4.0.1" | ||
|
||
typescript@^2.0.10: | ||
version "2.1.5" | ||
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.1.5.tgz#6fe9479e00e01855247cea216e7561bafcdbcd4a" | ||
|
||
uglify-js@^2.6: | ||
version "2.7.5" | ||
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.7.5.tgz#4612c0c7baaee2ba7c487de4904ae122079f2ca8" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missed this one as well