Skip to content

Commit 0725d62

Browse files
committed
Use Present<T> in nothing()/nil() return type
Fix TypeScript 5.3 compatibility. @perfective/common build fails since the TypeScript v5.3.0-dev.20230824. Type inference for the `nothing()` and `nil()` function has changed (probably caused by microsoft/TypeScript#54448). The code like: ``` const maybe: Maybe<number> = nothing(); ``` started to fail, as the `nothing()` return type is inferenced as `number | null | undefined` instead of just `number`. One solution is to explicitly set the type: ``` const maybe: Maybe<number> = nothing<number>(); ``` This solution may require significant amount of code updates across users' codebase. The applied alternative solution was to change the `nothing()` and `nil()` return types to `Nothing<Present<T>>`. In this case, the calls of `nothing()`/`nil()` remain as is, but `Maybe` and `Match` had to use additional type casts internally. This approach is chosen for the v0.10.0, so the code is compatible with TypeScript v5.3. It may be reconsidered in v0.11.0 if the root cause of the different type inference will be located.
1 parent aae9af8 commit 0725d62

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/match/match/match.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class Match<T> {
4141
return maybe(caseValue.statement(value));
4242
}
4343
}
44-
return nothing();
44+
return nothing() as Maybe<U>;
4545
}
4646
}
4747

src/maybe/maybe/maybe.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ export class Just<T> extends Maybe<T> {
287287
if (filter(this.value)) {
288288
return this;
289289
}
290-
return nothing<T>();
290+
return nothing<T>() as Maybe<T>;
291291
}
292292

293293
/**
@@ -305,7 +305,7 @@ export class Just<T> extends Maybe<T> {
305305
// Return it as is instead of passing it through just(this.value).
306306
return this as unknown as Just<U>;
307307
}
308-
return nothing<U>();
308+
return nothing<U>() as Maybe<U>;
309309
}
310310

311311
/**
@@ -321,7 +321,7 @@ export class Just<T> extends Maybe<T> {
321321
if (isTrue(condition)) {
322322
return this;
323323
}
324-
return nothing<T>();
324+
return nothing() as Maybe<T>;
325325
}
326326

327327
/**
@@ -566,9 +566,9 @@ export function maybe<T>(value: T | null | undefined): Maybe<T> {
566566
return just(value);
567567
}
568568
if (isNull(value)) {
569-
return nil();
569+
return nil() as Maybe<T>;
570570
}
571-
return nothing();
571+
return nothing() as Maybe<T>;
572572
}
573573

574574
/**
@@ -661,16 +661,16 @@ const memo: Memo = {
661661
*
662662
* @since v0.1.0
663663
*/
664-
export function nothing<T>(): Nothing<T> {
665-
return memo.nothing as Nothing<T>;
664+
export function nothing<T>(): Nothing<Present<T>> {
665+
return memo.nothing as Nothing<Present<T>>;
666666
}
667667

668668
/**
669669
* Returns a memoized instance of {@linkcode Nothing} with a `null` value.
670670
*
671671
* @since v0.10.0
672672
*/
673-
export function nil<T>(): Nothing<T> {
673+
export function nil<T>(): Nothing<Present<T>> {
674674
// eslint-disable-next-line deprecation/deprecation -- TODO(https://github.com/perfective/ts.common/issues/31)
675675
return naught();
676676
}
@@ -681,8 +681,8 @@ export function nil<T>(): Nothing<T> {
681681
* @deprecated Since v0.10.0. Use `nil` instead.
682682
* TODO(https://github.com/perfective/ts.common/issues/31).
683683
*/
684-
export function naught<T>(): Nothing<T> {
685-
return memo.nil as Nothing<T>;
684+
export function naught<T>(): Nothing<Present<T>> {
685+
return memo.nil as Nothing<Present<T>>;
686686
}
687687

688688
/**

0 commit comments

Comments
 (0)