Skip to content

Refactor: Make defineString generic for better type-safety #1693

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/params/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export function defineSecret(name: string): SecretParam {
* @param options Configuration options for the parameter.
* @returns A parameter with a `string` return type for `.value`.
*/
export function defineString(name: string, options: ParamOptions<string> = {}): StringParam {
export function defineString<T extends string>(name: string, options: ParamOptions<T> = {}): StringParam<T> {
const param = new StringParam(name, options);
registerParam(param);
return param;
Expand Down
14 changes: 10 additions & 4 deletions src/params/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,18 @@ export interface SelectOptions<T = unknown> {
value: T;
}

/**
* This can be removed once typescript is upgraded to v5.4+
* @internal
*/
type NoInfer<T> = [T][T extends any ? 0 : never];

/** The wire representation of a parameter when it's sent to the CLI. A superset of `ParamOptions`. */
export type ParamSpec<T extends string | number | boolean | string[]> = {
/** The name of the parameter which will be stored in .env files. Use UPPERCASE. */
name: string;
/** An optional default value to be used while prompting for input. Can be a literal or another parametrized expression. */
default?: T | Expression<T>;
default?: NoInfer<T> | Expression<NoInfer<T>>;
/** An optional human-readable string to be used as a replacement for the parameter's name when prompting. */
label?: string;
/** An optional long-form description of the parameter to be displayed while prompting. */
Expand Down Expand Up @@ -468,10 +474,10 @@ export class SecretParam {
* A parametrized value of String type that will be read from .env files
* if present, or prompted for by the CLI if missing.
*/
export class StringParam extends Param<string> {
export class StringParam<T extends string = string> extends Param<T> {
/** @internal */
runtimeValue(): string {
return process.env[this.name] || "";
runtimeValue(): T {
return process.env[this.name] as T;
}
}

Expand Down