-
-
Notifications
You must be signed in to change notification settings - Fork 9
feat: user-defined required constructor options #63
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4df5e57
build(test): run all code & TS tests in the root folder and all `exam…
gr2m e1aebc5
style: prettier
gr2m 61036c4
feat: required-options example
gr2m 8c777a4
Update examples/required-options/README.md
gr2m 74bdd51
I think it's all working now
JoshuaKGoldberg ba85870
fix(examples/required-options): check for type of required option ins…
gr2m 32744ac
Use undefined instead of never
JoshuaKGoldberg b9bfbc3
Merge branch '60-handle-user-defined-required-options' of https://git…
JoshuaKGoldberg 2ec3e41
Remove debug property
JoshuaKGoldberg 526b802
Update examples/required-options/index.d.ts
gr2m 8ee38c8
Update examples/required-options/index.test-d.ts
gr2m 5c4fdde
Update examples/required-options/README.md
gr2m 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 hidden or 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,29 @@ | ||
# required options Example | ||
|
||
`Base` has no required options by default, so the following code has no type errors. | ||
|
||
```js | ||
import { Base } from "javascript-plugin-architecture-with-typescript-definitions"; | ||
|
||
const base1 = new Base(); | ||
const base2 = new Base({}); | ||
``` | ||
|
||
But required options can be added by extending the `Base.Optiions` interface. | ||
|
||
```ts | ||
declare module "javascript-plugin-architecture-with-typescript-definitions" { | ||
namespace Base { | ||
interface Options { | ||
myRequiredUserOption: string; | ||
} | ||
} | ||
} | ||
``` | ||
|
||
With that extension, the same code will have type a type error | ||
gr2m marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```ts | ||
// TS Error: Property 'myRequiredUserOption' is missing in type '{}' but required in type 'Options' | ||
const base = new Base({}); | ||
``` |
This file contains hidden or 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,12 @@ | ||
import { Base } from "../../index.js"; | ||
export { Base } from "../../index.js"; | ||
gr2m marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
declare module "../.." { | ||
namespace Base { | ||
interface Options { | ||
myRequiredUserOption: string; | ||
} | ||
} | ||
} | ||
|
||
export class MyBase extends Base {} | ||
gr2m marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,13 @@ | ||
import { Base } from "../../index.js"; | ||
|
||
/** | ||
* @param {Base} base | ||
* @param {Base.Options} options | ||
*/ | ||
function pluginRequiringOption(base, options) { | ||
if (!options.myRequiredUserOption) { | ||
throw new Error('Required option "myRequiredUserOption" missing'); | ||
} | ||
} | ||
|
||
export const MyBase = Base.plugin(pluginRequiringOption); |
This file contains hidden or 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,11 @@ | ||
import { MyBase } from "./index.js"; | ||
|
||
// @ts-expect-error - An argument for 'options' was not provided | ||
new MyBase(); | ||
|
||
// @ts-expect-error - Type '{}' is missing the following properties from type 'Options': myRequiredUserOption | ||
new MyBase({}); | ||
|
||
new MyBase({ | ||
myRequiredUserOption: "", | ||
}); | ||
gr2m marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,18 @@ | ||
import { test } from "uvu"; | ||
import * as assert from "uvu/assert"; | ||
|
||
import { MyBase } from "./index.js"; | ||
|
||
test("new MyBase()", () => { | ||
assert.throws(() => new MyBase()); | ||
}); | ||
|
||
test("new MyBase({})", () => { | ||
assert.throws(() => new MyBase({})); | ||
}); | ||
|
||
test('new MyBase({ myRequiredUserOption: ""})', () => { | ||
assert.not.throws(() => new MyBase({ myRequiredUserOption: "" })); | ||
}); | ||
|
||
test.run(); |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.