Skip to content

Commit 5e88ed1

Browse files
refactor(types): make defineString generic for better type-safety
1 parent 534505c commit 5e88ed1

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/params/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export function defineSecret(name: string): SecretParam {
130130
* @param options Configuration options for the parameter.
131131
* @returns A parameter with a `string` return type for `.value`.
132132
*/
133-
export function defineString(name: string, options: ParamOptions<string> = {}): StringParam {
133+
export function defineString<T extends string>(name: string, options: ParamOptions<T> = {}): StringParam<T> {
134134
const param = new StringParam(name, options);
135135
registerParam(param);
136136
return param;

src/params/types.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,18 @@ export interface SelectOptions<T = unknown> {
293293
value: T;
294294
}
295295

296+
/**
297+
* This can be removed once typescript is upgraded to v5.4+
298+
* @internal
299+
*/
300+
type NoInfer<T> = [T][T extends any ? 0 : never];
301+
296302
/** The wire representation of a parameter when it's sent to the CLI. A superset of `ParamOptions`. */
297303
export type ParamSpec<T extends string | number | boolean | string[]> = {
298304
/** The name of the parameter which will be stored in .env files. Use UPPERCASE. */
299305
name: string;
300306
/** An optional default value to be used while prompting for input. Can be a literal or another parametrized expression. */
301-
default?: T | Expression<T>;
307+
default?: NoInfer<T> | Expression<NoInfer<T>>;
302308
/** An optional human-readable string to be used as a replacement for the parameter's name when prompting. */
303309
label?: string;
304310
/** An optional long-form description of the parameter to be displayed while prompting. */
@@ -468,10 +474,10 @@ export class SecretParam {
468474
* A parametrized value of String type that will be read from .env files
469475
* if present, or prompted for by the CLI if missing.
470476
*/
471-
export class StringParam extends Param<string> {
477+
export class StringParam<T extends string = string> extends Param<T> {
472478
/** @internal */
473-
runtimeValue(): string {
474-
return process.env[this.name] || "";
479+
runtimeValue(): T {
480+
return process.env[this.name] as T;
475481
}
476482
}
477483

0 commit comments

Comments
 (0)