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

Improve types #1290

Open
simenandre opened this issue Dec 27, 2022 · 1 comment
Open

Improve types #1290

simenandre opened this issue Dec 27, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@simenandre
Copy link

Thank you for this nice product (and toolkit).

To improve use with Typescript I have a few improvements that can help developer experience and stability of products based on @actions/core. The key thing here is the signature of any input function, such as getInput, getMultilineInput etc.

Let's look at the getInput signature: getInput(name: string, options?: InputOptions): string. In InputOptions we have a required: boolean option we can set. This is nicely built to throw when the input is missing, which is nice.

A way we can improve this is by casting whether the input is required or not. Because right now, getInput is returning string but defaults to required being false, which means that developers might expect a value but there is none. We can solve this by some using function overloading.

/**
 * Interface for getInput options
 */
export interface InputOptions {
  /** Optional. Whether the input is required. If required and not present, will throw. Defaults to false */
  required?: boolean

  /** Optional. Whether leading/trailing whitespace will be trimmed for the input. Defaults to true */
  trimWhitespace?: boolean
}

export interface RequiredInputOptions extends InputOptions {
	required: true;
}

export interface OptionalInputOptions extends InputOptions {
	required: false;
}

export function getInput(name: string, options: RequiredInputOptions): string
export function getInput(name: string, options: OptionalInputOptions): string | undefined
export function getInput(name: string, options?: InputOptions): string { ... }

This might look more bloaty in the types / code for @actions/core, but will improve what the developer sees when using it.

Check out this example: https://codesandbox.io/s/actions-core-improvements-o6stpw?file=/src/index.ts

My motivation is trying to remove some of the heavy boilerplate we've got over at Pulumi Actions. We're using runtypes to validate our configuration, which is quite cumbersome compared to using only this package :)

Again, thank you so much!

@simenandre simenandre added the enhancement New feature or request label Dec 27, 2022
@simenandre
Copy link
Author

If anyone has a better idea, please share :)

@simenandre simenandre changed the title Improve typecasting Improve types Dec 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant