-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
32af6a3
commit d2d4368
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 @@ | ||
/** | ||
Cast a value to the given type safely. | ||
The `as` keyword allows unsafe conversions between unrelated types, like converting a number to a string. This function restricts casting to compatible types, preserving type safety. | ||
@example | ||
``` | ||
type Foo = { | ||
a: string; | ||
b?: number; | ||
}; | ||
declare const possibleUndefined: Foo | undefined; | ||
const foo = possibleUndefined ?? safeCastTo<Partial<Foo>>({}); | ||
console.log(foo.a ?? '', foo.b ?? 0); | ||
const bar = possibleUndefined ?? {}; | ||
// @ts-expect-error | ||
console.log(bar.a ?? '', bar.b ?? 0); | ||
// ^^^ Property 'a' does not exist on type '{}'.(2339) | ||
// ^^^ Property 'b' does not exist on type '{}'.(2339) | ||
``` | ||
@category General | ||
*/ | ||
export function safeCastTo<T>(value: T): T { | ||
return value; | ||
} |
This file contains 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,38 @@ | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
import test from 'ava'; | ||
import {expectTypeOf} from 'expect-type'; | ||
import {safeCastTo} from '../source/index.js'; | ||
|
||
test('safeCastTo()', t => { | ||
type Foo = { | ||
a: string; | ||
b?: number; | ||
}; | ||
|
||
const EmptyObject = {}; | ||
const foo: Foo = { | ||
a: '', | ||
b: 0, | ||
}; | ||
|
||
t.is(EmptyObject, safeCastTo(EmptyObject)); | ||
t.is(foo, safeCastTo(foo)); | ||
|
||
expectTypeOf({}).toEqualTypeOf<{}>(); | ||
expectTypeOf(safeCastTo({})).toEqualTypeOf<{}>(); | ||
expectTypeOf({}).not.toEqualTypeOf<Partial<Foo>>(); | ||
expectTypeOf(safeCastTo({})).not.toEqualTypeOf<Partial<Foo>>(); | ||
expectTypeOf(safeCastTo<Partial<Foo>>({})).toEqualTypeOf<Partial<Foo>>(); | ||
expectTypeOf(safeCastTo<Partial<Foo>>({}).a).toEqualTypeOf<string | undefined>(); | ||
expectTypeOf(safeCastTo<Partial<Foo>>({}).b).toEqualTypeOf<number | undefined>(); | ||
|
||
expectTypeOf(foo).toEqualTypeOf<Foo>(); | ||
expectTypeOf(safeCastTo(foo)).toEqualTypeOf<Foo>(); | ||
expectTypeOf(safeCastTo<Partial<Foo>>(foo)).not.toEqualTypeOf<Foo>(); | ||
expectTypeOf(safeCastTo<Partial<Foo>>(foo)).toEqualTypeOf<Partial<Foo>>(); | ||
|
||
// @ts-expect-error | ||
safeCastTo<Foo>({}); | ||
// @ts-expect-error | ||
safeCastTo<Required<Foo>>(foo); | ||
}); |