-
-
Notifications
You must be signed in to change notification settings - Fork 231
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
Implement Struct.get #1890
Comments
IME it's not easy to get the typing right: function get<T extends string>(key: T): <S extends Record<T, unknown>>(s: S) => S[T] {
return (s) => {
return s[key]
}
}
const record: Record<string, number> = {}
// const value: number
const value = pipe(record, get("a")) |
Good point, I haven’t thought about this. But I guess from a typing perspective it would be solvable to detect if the key matches the key of an index signature: function get<T extends string>(
key: T
): <S extends Record<T, unknown>>(s: S) => {} extends Record<keyof S, unknown> ? S[T] | undefined : S[T] {
return (s) => {
return s[key];
};
}
const record: Record<string, number> = {}
// const value: number | undefined
const value = pipe(record, get("a")) But that requires us to deal with possible FWIW: I’ve checked how Regarding import * as S from '@effect/schema/Schema';
const mySchema = S.record(S.string, S.number);
const onlyAgeSchema = mySchema.pipe(S.pick('age'));
type OnlyAge = S.Schema.To<typeof onlyAgeSchema> // type { age: number } |
actually it seems that import { pipe } from "effect/Function"
import * as Struct from "effect/Struct"
const record: Record<string, number> = {}
/*
const value: {
a: number;
b: number;
}
*/
const value = pipe(record, Struct.pick("a", "b")) |
Wow, that was fast. Thank you for working on this 🙇. |
Regarding So this is ok import * as S from "@effect/schema/Schema"
const schema = S.record(S.string, S.number)
const OnlyAge = schema.pipe(S.pick("age"))
type OnlyAge = S.Schema.To<typeof OnlyAge> // type { age: number } but the current produced console.log(OnlyAge.ast) // wrong AST
/*
{
_tag: 'TypeLiteral',
propertySignatures: [],
indexSignatures: [],
annotations: {}
}
*/ should be {
_tag: 'TypeLiteral',
propertySignatures: [
{
name: 'age',
type: {
_tag: 'NumberKeyword',
annotations: {
[Symbol(@effect/schema/annotation/Title)]: 'number',
[Symbol(@effect/schema/annotation/Description)]: 'a number'
}
},
isOptional: false,
isReadonly: false,
annotations: {}
},
[length]: 1
],
indexSignatures: [ [length]: 0 ],
annotations: {}
} |
btw curious that despite starting from a readonly record a mutable type is returned: type A = Pick<{ readonly [x: string]: number }, "age">
/*
type A = {
age: number;
}
*/ |
In this case you are right, the schema is behaving correctly. IMHO the behavior of the built-in |
What is the problem this feature would solve?
Similar to
Struct.pick
it would be nice to have a util function that can extract the value of a single property within the given object. This is useful when iterating over an array of objects in order to extract just one property, for example:What is the feature you are proposing to solve the problem?
The implementation could look like this
Example Usage:
What alternatives have you considered?
No response
The text was updated successfully, but these errors were encountered: