-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Design Meeting Notes, 10/18/2023 #56158
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
Comments
This was always surprising to me too, because |
|
Vue 3 refs/reactives provide automatically unboxing of deep refs: const a = reactive({ foo: 1 })
a.foo // 1
const foo2 = ref(2)
// valid
a.foo = foo2;
a.foo // 2
a.foo = 3
foo2.value // 3 typescript playground In regards to the options, I think either no colon and accessor would be fine. type Foo<T> = {
[K in keyof T] { // wait, what if people accidentally put a colon here? or forget one?
get(): T;
set(value: T[K] | undefined);
}
} If they forget the colon it becomes an object with 2 function properties :D |
I see, so you want to permit a write of a Would it be described as something like the following? type Ref<T> = {
[K in keyof T] {
get(): T[K];
set(value: T[K] | Reactive<T[K]>);
}
} |
@DanielRosenwasser exactly |
(I've amended my comment to have an example of the type itself) |
@DanielRosenwasser roughly that, naming would be a bit different, I think it could be described like this: // ref value is access via .value, to allow assign primitives
type Ref<T> = { value: T }
// Unbox ref
type Unref<T> = T extends Ref<infer R> ? R : T;
type Reactive<T extends Record<PropertyKey, any>> = {
[ K in keyof T] {
// we need to deep unboxing the type
get(): T[K] extends object ? Unref<Reactive(T[K])> : T[K];
set<V extends Unref<Reactive<T[K]>> | Reactive<T[K]> | Ref<T[K]>>(value: V): V; // we return the same value that was set
}
// example
const msg : Ref<string> = ref('Hello World!')
const a: Reactive<{ msg: string }> = reactive({ msg })
console.log(a.msg = ref('random')) // ref('random')
console.log(a.msg) // "random" That's the current behaviour at runtime. playground While building this might be useful to allow the EDIT: Made a few edits on the code, this is to show the object is set to type Reactive<T extends Record<PropertyKey, any>> = {
[ K in keyof T] {
// in case the T[K] is a ref
get(): Unref<T[K]>;
set<V extends Unref<T[K]> | T[K] | Ref<T[K]>>(value: V): V; // we return the same value that was set
} EDIT2: Not a Vue use case AFAIK, but supporting overrides might be useful, although besides possibly making the type Foo<T> = {
[ K in keyof T] {
get(): T[K]
// set overloads
set(value: Unref<T[K]>): Unref<T[K]>
set(value: Reactive<T[K]>): Reactive<T[K]>
set(value: Ref<T[K]>): Ref<T[K]>
// etc
}
} |
Varying Types for Index Signatures and Mapped Types
#43826
Today, TypeScript supports separate read and write types.
Some feedback requesting the ability to do this with index signatures and mapped types.
How do we feel about the idea?
Don't like need to repeat the
[K in keyof T]
for eachget
andset
type for mapped type.Another proposal - grouped
get
andset
for mapped types.Okay, how about the
accessor
keyword?Use case is a map that's "optional on the write, always present on the read"
Do the names have to be the same between
get
andset
index signatures?get
vs.readonly
index signatures?Do other properties get validated against
set
index signatures?get
/set
?What are the use-cases?
Conclusion?
Unrelated Interface Causes Assignability Differences
#56099
any
is not assignable tonever
- kind of surprising.DefinitelyTyped is a Monorepo!
DefinitelyTyped/DefinitelyTyped#67085
package.json
with versions and stuff!tsconfig.json
!@types/node
)events
versus npm libraryevents
The text was updated successfully, but these errors were encountered: