-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(types): enforce or infer whether properties are required. (#114)
* Add optional generic constraint for FSA type property * Get jest to also test TypeScript tests * Optional Payload type constraint * Add comments to FSA types and properties * Add FSA extensions with required properties * Simplified FSA TypeScript test Co-Authored-By: couven92 <[email protected]> * fixup! Simplified FSA TypeScript test Fix and refactor TypeScript tests * Add test for FSAAuto type * Update Jest and ESLint dev dependencies * Move generic argument Type to the front for all FSA types
- Loading branch information
Showing
7 changed files
with
1,666 additions
and
1,040 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
transform: { | ||
'^.+\\.js$': 'babel-jest', | ||
'^.+\\.tsx?$': 'ts-jest' | ||
}, | ||
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$', | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'] | ||
}; |
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,16 @@ | ||
import { FSAAuto } from '../src'; | ||
|
||
describe('Usage of FSAAuto (automatically infer required properties', () => { | ||
it('must specify payload property even when using a union with undefined', () => { | ||
const fsa_with_payload = { type: 'TEST', payload: undefined }; | ||
expectOptionalPayload(fsa_with_payload); | ||
|
||
const fsa_without_payload = { type: 'TEST' }; | ||
// Not possible to cast!!! | ||
// expectOptionalPayload(fsa_without_payload); | ||
|
||
function expectOptionalPayload(fsa: FSAAuto<string, string | undefined>) { | ||
expect(fsa.payload).toBeUndefined(); | ||
} | ||
}); | ||
}); |
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,23 @@ | ||
import { FSA } from '../src'; | ||
|
||
const ACTION_TYPE_1 = 'ACTION_TYPE_1'; | ||
type ACTION_TYPE_1 = typeof ACTION_TYPE_1; | ||
type FSA_ACTION_TYPE_1 = FSA<ACTION_TYPE_1>; | ||
|
||
const assertNever = (x: never): never => { | ||
throw new Error(`Unexpected value: ${x}.`); | ||
}; | ||
|
||
const assertTypeValue = (fsa: FSA_ACTION_TYPE_1) => { | ||
expect(fsa.type).toBe(ACTION_TYPE_1); | ||
}; | ||
|
||
describe('FluxStandardAction<Payload, Meta, Type>', () => { | ||
it('enables TypeScript action type enforcement', () => { | ||
const fsa_strict: FSA_ACTION_TYPE_1 = { type: ACTION_TYPE_1 }; | ||
assertTypeValue(fsa_strict); | ||
if (fsa_strict.type !== ACTION_TYPE_1) { | ||
throw assertNever(fsa_strict.type); | ||
} | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -4,7 +4,5 @@ | |
"strictNullChecks": true, | ||
"target": "es5" | ||
}, | ||
"files": [ | ||
"test/typings.test.ts" | ||
] | ||
"files": ["src/index.d.ts"] | ||
} |
Oops, something went wrong.