-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Design Meeting Notes, 7/28/2017 #17502
Comments
Since there's no issues linked to 'Readonly & Variance' section (you may want to link #1394, #10717), I'll leave a few comments here. I claim that in some cases, it is not obvious or even impossible to be express variance with interface Transformer<I, O> {
transform(value: I): O;
transformArray(values: I[]): O[];
} What would In #10717 I was proposing both co- and contra- varainces on generic types for both declaration and use sites: interface Array<T> {
[index: number]: T;
push(value: T): void;
pop(): T;
}
type ReadonlyArray<T> = Array<out T>;
// is equivalent to this
interface ReadonlyArray<T> {
readonly [index: number]: T;
// push(value: T): void;
pop(): T;
}
type WriteonlyArray<T> = Array<in T>;
// is equivalent to this
interface WriteonlyArray<T> {
writeonly [index: number]: T; // no such syntax
push(value: T): void;
// pop(): T;
} |
@Igorbek I've been thinking more about use-site variance recently as well. @ahejlsberg will be able to give more direct context, but I'll try answering your questions. In general, the connection is that the As an example, look at how we've had to create To be clear, we are still experimenting with these ideas. It's not even clear whether stricter variance is something we're committed to, but I'm glad you asked. |
Edit: I just noticed you pointed part of this out when you wrote:
By the way, I want to point out that for many containers, a So neither |
Thank you @DanielRosenwasser for answering. That actually was my point that they, read-only-fity and covariance, are not always connected. interface Transformer<I, O> {
transform(value: I): O;
transformArray(values: in I[]): out O[];
}
// or, as a shortcut for the same
interface Transformer<in I, out O> {
transform(value: I): O;
transformArray(values: I[]): O[];
} , or, in a more simplified case, I could just mark only interface Transformer<I, O> {
transform(value: I): O;
transformArray(values: in I[]): O[];
}
declare const t1: Transformer<string, 'a'>;
const t2: Transformer<'b', 'a'> = t1; // ok
const o2 = t2.transformArray(['b']);
o2.push('a'); // ok
const t2: Transformer<string, string> = t1; // allowed, but! O is implicitly out
const o2 = t2.transformArray(['b']);
o2.push('b'); // error, method pushed is excluded (kinda random thoughts) |
Just 🚲:house:ing, but while I know TypeScript has been sticking to any established JavaScript precedents, wherever available, when adopting type level syntax ( |
I propose that we continue the discussion of "Readonly & Variance" in #18770. |
f# Readonly on Exports from Namespaces (#16720)
Readonly & Variance
One thing that comes up quite a bit is strict variance.
If we enabled stricter variance, that would imply that
Array
becomes invariant.ReadonlyArray
.T
andReadonlyT
Strict variance is easy to enable, but would be hard to make easy to use.
This leads us to start thinking about how to quickly make a readonly version of a type.
We've had an experimental type operator called
readonly
keyof T
, you just writereadonly T
.Like the
Readonly
mapped type, it makes all propertiesreadonly
as well.Readonly<T>
mapped type doesn't eliminate mutating methods at all.This means we needed to add a way to indicate that a method doesn't mutate its object.
this: readonly
syntax.Example:
What about deep readonly?
readonly Foo[]
give you areadonly Array<readonly Foo>
?immutable
type operatorIn
--strictReadonly
mode, thisreadonly
operator would work as described above.readonly T
not assignable toT
.readonly
props would not be assignable to mutable props.readonly T
removes mutating methodsOutside of
strictReadonly
,readonly T
is justReadonly<T>
All of this means that library authors would need to enable readonly mode.
Would TypeScript be able to infer whether a method is readonly or not?
How bad are strict variance modes without this?
super
constraint for generics - otherwise many types stay invariant by virtue of taking aT
as an input position.Will people know how to use
this: readonly
?.d.ts
files.readonly HTMLElement
?How do
this: readonly
parameters work with overloads?this: readonly
, the property is eliminated.The text was updated successfully, but these errors were encountered: