-
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
Allow to directly reference the types associated with interface properties #4555
Comments
I'll try to state it more formally: The access path, starting with the name of the type e.g. If that syntax appears to confuse with instance access operations (I'm not sure at this moment), it is possible to require the Mentioning interface A {
prop: {
x: number;
y: number;
}
}
let a: A;
let p: typeof a.prop; // OK, type of p is { x: number, y: number } But that looks a bit like a hack to me.. (not a good practice and not something I would recommend as a standard solution). What this may imply though is that this proposal may not be Too Complex™ because that "hack" could be used internally in the compiler to retrieve the type (at least in a large part of the cases). There are also places where it would be impossible to use dummies or would look really bad: like in outermost types (dummies would either be impossible or need to be global variables), declaration files (dummies would look inappropriate for a "formal" declaration) etc. So, in the light of this I would say that it still seems reasonable and more natural to me to have a way to directly access types of properties from the containing type itself, rather than indirectly through an instance, and that may not turn out to be "Too Complex" to implement, but I'm not sure right now about what is the best, most readable syntax to for it. |
Some creative (and unexpected) applications of this (which also wouldn't be possible with the Directly referencing the type of a different property of the containing interface in a method's argument or return type. interface Cool {
val: SomeType;
// The return type would automatically "sync" with whatever type "val" has been set to!
func(arg: number): Cool.val;
} Automatic type matching between interface properties (both in the same one and on different ones): interface A {
prop: {
subProp: {
x: number,
y: number,
str: string
}
}
}
interface B {
// would automatically "sync" with changes to the type of A.prop.subProp
matchedProp: A.prop.subProp;
} I will post others when I can think of more, but I already find these interesting. |
This proposal would be ambiguous given the current language semantics. consider the case with a module with a property with the same name, e.g.: interface A {
I: string;
}
namespace A {
export interface I {}
}
var x: A.I; // A.I can not resolve to property I of interface A, as it already resolves to something else. I believe what you are asking for is more along the lines of operators like |
Having an interface and a namespace with the same name is a "corner" of the language that I'm not very familiar with (I do think I encountered it once or twice so I had a feeling it exists). I wasn't very sure if having a direct reference like I believe I can write a proposal for this. I'm not sure if I could cover everything but I'll try as much as I can. |
I considered several approaches to the syntax. One was without a keyword, using something like Then I went back to look for a keyword but wanted something that's shorter than I came across the idea of using interface MyInterface {
myProp: {
x: number,
y: number
}
}
let x: typeon MyInterface.myProp; The idea is that the operator queries for the type is set "on" that property, instead of belonging to it ("of") as would be with an instance. This may not be perfect but I went through an exhaustive list of prepositions in English (and another one here). The only somewhat reasonable alternatives that I could find were "in" (i.e. It is still possible there are better options so if you (or anyone else) have alternative ideas then please suggest. [edit: |
The proposal is in #4640, closing. |
Trying to associate the type of
prop
with a variable would lead to an error:Aside from anonymous object interfaces, it could also be allowed for function types:
Primitive types:
And also when the types are nested:
The current workaround (the best that could I find, at least [edit: I added an additional one that uses
typeof
in the next comment]) is to define the type ofprop
as a named interface or alternatively use thetype
keyword.But that isn't always necessary or elegant, especially if the types are nested or generic (short example below).
This can also be extended to types defined through the
type
keyword or when usingtypeof SomeClass
. Or even with generics:A real-world use case I encountered was casting a value into the type of an interface member from a union that includes it (this happened when thinking about issues with assignability of merged/intersected types):
@RyanCavanaugh Too Complex? Perhaps at least keep it as an option for the future?
The text was updated successfully, but these errors were encountered: