Skip to content

Commit 2251b15

Browse files
authoredFeb 26, 2025··
Schema: More Accurate Return Type for parseNumber (#4524)
1 parent ce059f2 commit 2251b15

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed
 

‎.changeset/fresh-turtles-fix.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Schema: More Accurate Return Type for `parseNumber`.
6+
7+
**Before**
8+
9+
```ts
10+
import { Schema } from "effect"
11+
12+
const schema = Schema.parseNumber(Schema.String)
13+
14+
// ┌─── Schema<string>
15+
//
16+
schema.from
17+
```
18+
19+
**After**
20+
21+
```ts
22+
import { Schema } from "effect"
23+
24+
const schema = Schema.parseNumber(Schema.String)
25+
26+
// ┌─── typeof Schema.String
27+
//
28+
schema.from
29+
```

‎packages/effect/dtslint/Schema/Schema.tst.ts

+12
Original file line numberDiff line numberDiff line change
@@ -4062,5 +4062,17 @@ describe("Schema", () => {
40624062
expect(pipe(S.Struct({ a: S.optionalWith(S.String, { exact: true }), b: S.Number }), S.pluck("a")))
40634063
.type.toBe<S.SchemaClass<string | undefined, { readonly a?: string }>>()
40644064
})
4065+
4066+
it("parseNumber", () => {
4067+
// @ts-expect-error: Type 'null' is not assignable to type 'string'
4068+
S.parseNumber(S.Null)
4069+
4070+
const schema = S.parseNumber(S.String)
4071+
expect(S.asSchema(schema)).type.toBe<S.Schema<number, string>>()
4072+
expect(schema).type.toBe<S.transformOrFail<typeof S.String, typeof S.Number>>()
4073+
expect(schema.annotations({})).type.toBe<S.transformOrFail<typeof S.String, typeof S.Number>>()
4074+
expect(schema.from).type.toBe<typeof S.String>()
4075+
expect(schema.to).type.toBe<typeof S.Number>()
4076+
})
40654077
})
40664078
})

‎packages/effect/src/Schema.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -5262,21 +5262,23 @@ export const clamp = (minimum: number, maximum: number) =>
52625262
* @category number transformations
52635263
* @since 3.10.0
52645264
*/
5265-
export const parseNumber = <A extends string, I, R>(
5266-
self: Schema<A, I, R>
5267-
): transformOrFail<Schema<A, I, R>, typeof Number$> =>
5268-
transformOrFail(
5265+
export function parseNumber<S extends Schema.Any, A extends string>(
5266+
self: S & Schema<A, Schema.Encoded<S>, Schema.Context<S>>
5267+
): transformOrFail<S, typeof Number$> {
5268+
return transformOrFail(
52695269
self,
52705270
Number$,
52715271
{
52725272
strict: false,
52735273
decode: (i, _, ast) =>
5274-
ParseResult.fromOption(number_.parse(i), () =>
5275-
new ParseResult.Type(ast, i, `Unable to decode ${JSON.stringify(i)} into a number`)),
5276-
encode: (a) =>
5277-
ParseResult.succeed(String(a))
5274+
ParseResult.fromOption(
5275+
number_.parse(i),
5276+
() => new ParseResult.Type(ast, i, `Unable to decode ${JSON.stringify(i)} into a number`)
5277+
),
5278+
encode: (a) => ParseResult.succeed(String(a))
52785279
}
52795280
)
5281+
}
52805282

52815283
/**
52825284
* This schema transforms a `string` into a `number` by parsing the string using the `parse` function of the `effect/Number` module.

0 commit comments

Comments
 (0)
Please sign in to comment.