- Notifications
You must be signed in to change notification settings - Fork13.2k
Variant accessors#42425
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Variant accessors#42425
Uh oh!
There was an error while loading.Please reload this page.
Conversation
RyanCavanaugh commentedJan 20, 2021
@typescript-bot pack this |
typescript-bot commentedJan 20, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@RyanCavanaugh, I've started to run the tarball bundle task on this PR at6f09705. You can monitor the buildhere. |
typescript-bot commentedJan 20, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Hey@RyanCavanaugh, I've packed this intoan installable tgz. You can install it for testing by referencing it in your and then running There is also a playgroundfor this build and annpm module you can use via |
RyanCavanaugh commentedJan 29, 2021
@typescript-bot pack this |
typescript-bot commentedJan 29, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@RyanCavanaugh, I've started to run the tarball bundle task on this PR atd2affa2. You can monitor the buildhere. |
typescript-bot commentedJan 29, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Hey@RyanCavanaugh, I've packed this intoan installable tgz. You can install it for testing by referencing it in your and then running There is also a playgroundfor this build and annpm module you can use via |
ExE-Boss commentedFeb 5, 2021
This probably also fixes #32821 |
trusktr commentedFeb 17, 2021
What milestone will this go into? |
…ariant accessor types in TypeScriptThis make accessors have the correct types to represent all possible values that they can be set to, but the getters are funky because to use them it requires type casting. As a temporary workaround, `get*()` methods have been added (f.e. `element.getPosition()` returns the same as `element.position` but with the value casted to the underlying object type.This does not impact JavaScript users. They can continue to do things like `element.position.x = 123`. But TypeScript users can't do that. They would have to write `;(element.position as XYZNumberValues).x = 123` which is cumbersome, but the temporary getter methods allow TypeScript users to write `element.getPosition().x = 123` instead. TypeScript users can still set values as usual: `this.position = [1, 2, 3]` just like JavaScript users.The upcoming changes inmicrosoft/TypeScript#42425 will allow us to update the getter types so TypeScript users can write `element.position.x = 123`.
d2affa2 tob7f93bbCompareRyanCavanaugh commentedMar 25, 2021
@typescript-bot pack this |
typescript-bot commentedMar 25, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@RyanCavanaugh, I've started to run the tarball bundle task on this PR atb7f93bb. You can monitor the buildhere. |
typescript-bot commentedMar 25, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Hey@RyanCavanaugh, I've packed this intoan installable tgz. You can install it for testing by referencing it in your and then running There is also a playgroundfor this build and annpm module you can use via |
# Conflicts:#src/compiler/checker.ts#src/compiler/diagnosticMessages.json#tests/baselines/reference/privateNamesAndGenericClasses-2.errors.txt
This will depend onmicrosoft/TypeScript#42425 landing in stable TypeScript so we can update the types to reflect it.
kevinclarkadstech commentedApr 14, 2021
@typescript-bot pack this |
pikax commentedApr 26, 2021
What's the syntax (if supported) for generic types? typeMarkAsString<TextendsRecord<string,any>>={[KinkeyofT]:Textendsnumber ?T[K]/* How to set add a string|number set here? */ :T[K]}declareconsta:MarkAsString<{a:boolean,b:number,c:string}>a.b='42';// I want to have this avaliblea.c='112'//@ts-expect-errora.a='2' Actual use caseIn Vue3 there's constr=ref({a:1})r.value.a=1consta=reactive(r)a.a// 1 On a constr=ref({a:1});consta=ref({ r})// results in `{ r: { a: 1 }}`a.r=r;// typescript error because `r` is `Ref<T>` instead of `UnwrappedRef<T>`, but it works at runtime. |
thw0rted commentedApr 26, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I think what you're actually looking for is how to use the new feature with index signatures. My first guess would have been something like but that doesn't work on the latest Playground for this build. Maybe it's not currently possible? |
RyanCavanaugh commentedApr 26, 2021
Yeah, that's a totally separate and additional feature. |
pikax commentedApr 26, 2021
Cool, should I open an issue? |
RyanCavanaugh commentedApr 26, 2021
Sure |
V1raNi commentedAug 4, 2021
This is very cool, is there any information on which version will have this change? |
yume-chan commentedAug 5, 2021
ws93 commentedAug 7, 2021
The different type for setter doesn't work for Union type as well. Is this expected? Example can be foundhere |
thw0rted commentedAug 9, 2021
V1raNi commentedAug 11, 2021
Oh, thank you very much. It's just I was interested in different visibility and couldn't find it in the text, and the docs seem to be outdated a bit regarding this part. |
Uh oh!
There was an error while loading.Please reload this page.
Getter / Setter Variation
Implements#2845 and#2521
Overview
This implements two related features:
Differing Types
Property getters and setters may now differ in their types:
Restrictions
As a conservative implementation, a few restrictions are in place.
First, the type of the getter must be assignable to the type of the setter. In other words, the assignment
must be legal assuming
obj.xis writable at all.This restriction closes off a certain set of use cases for properties that start out "uninitialized" (usually
null/undefined) but then only want "initalizing" assignments to occur. We'll continue to examine these to see if those use cases are prevelant enough to warrant opening this up more.These prevent novel unsoundness from occurring and makes this feature unimpactful from a type relational perspective, which limits its risk.
Differing Visibility
In classes,
setaccessors may now be less visible than their correspondinggetaccessor. It looks like this:Type Relationship Effects
TL;DR: there are none
TypeScript is already covariant when relating properties of types. The unsoundness of this is straightforward to demonstrate during aliased writes:
TypeScript also already ignores the
readonlymodifier when relating types, so theprotectedorprivatestate of a setter does follows the same pattern.Caveats
TL;DR: The type system remains entirely covariant in other operations! This has some effects that may not be immediately apparent.
Types with variant getters/setters are effectively reduced to their
getside when put through mapped types:This is fairly straightforward to reason about -- a mapped type usually represents a "copy with transform" operation, e.g.
In other words, for an arbitrary mapped type, we have no idea how its actual value is produced, and the only sound assumption is that this typedoesn't copy over any coercing semantics from its setters.
The same applies to lookup types -- the type
T[K]still means *theread type ofKonT. A freesetterfunction can't be used to indirectly access the coercing side of the setter: