-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
typeof this.xxx gives "identifier expected" error. #1554
Comments
i would say neither... it's just wrong syntax... try " var copy = typeof (this.data = { });"... having said this, i would not use this code... it's confusing... |
@giancarloa hmm, you shouldn't add those brackets. the code above is just a minimum issue trigger, not code in production, there would be some situations that you may want to use code like this. here if you take class Test {
static data = {};
constructor() {
var copy: typeof Test.data = {};
}
} |
As per the spec
So this is expected. I think the reason it isn't allowed is that something like this function foo() {
var x: typeof this.x;
} is perfectly valid, because |
@DanielRosenwasser O.O hmm... then hoping you would consider updating the spec. lol. |
It's something to consider; if your members are not private, you can easily get around this with the following in the mean time. self = this;
var x: typeof self.data; |
Waiting for this feature. And a recursive typeof member should lead to error. Thanks. |
Seems like this should just work if we changed the grammar. |
@RyanCavanaugh, but is it appropriate to allow it in the context I gave? It will inevitably lead to people doing so and getting |
syntactically correct doesn't have to mean that it should actually make sense, IMO. |
We already let you write |
Just to add another use case. Some time ago I made the type definitions for lodash and some of the supported signatures are really aliases for other methods. I had declared them like this: interface LoDashArrayWrapper<T> {
rest(): LoDashArrayWrapper<T>;
rest(
callback: ListIterator<T, boolean>,
thisArg?: any): LoDashArrayWrapper<T>;
rest(n: number): LoDashArrayWrapper<T>;
rest(pluckValue: string): LoDashArrayWrapper<T>;
rest(whereValue: {}): LoDashArrayWrapper<T>;
drop: typeof rest;
tail: typeof rest;
} This worked alright with version 1.0 of the compiler. But it doesn't compile any longer. And I don't see any other option besides duplicating the whole set of signatures for each alias as I cannot think of a TypeQueryExpression that expresses this. |
@juanevp How about function interface ? interface LoDashArrayWrapper<T> {
rest: LoDashArrayWrapperOperation<T>;
drop: LoDashArrayWrapperOperation<T>;
tail: LoDashArrayWrapperOperation<T>;
}
interface LoDashArrayWrapperOperation<T> {
(): LoDashArrayWrapper<T>;
(
callback: ListIterator<T, boolean>,
thisArg?: any): LoDashArrayWrapper<T>;
(n: number): LoDashArrayWrapper<T>;
(pluckValue: string): LoDashArrayWrapper<T>;
(whereValue: {}): LoDashArrayWrapper<T>;
} |
Approved. Should be extremely easy fix? Make sure this doesn't cause |
Is this related? I get
It would be useful to be able to use |
Hmmm I've just run into this problem :( I'm defining an object in my class.
In this case it is not appropriate to just change thing to a static variable and say @RyanCavanaugh @DanielRosenwasser @mhegazy sounds like this was thought to be a relatively easy fix? Looks like it's slipped away from attention over the years though... Any chance of it being implemented? |
@sam-s4s nowdays you aught to be able to write |
Thanks @weswigham - I did find out that I can do I could not find any combination using |
I have just tried, but it doesn't work in TypeScript 3.4.4 though, @sam-s4s 's one works ( |
If I have something like public groupByTypes = ['None', 'Foo', 'Bar'] as const;
public groupBy: typeof groupByTypes[number]; What is the proposed workaround? And I cannot make |
OMG, 7 years old design bug. Ok, see what would be after yet another 7. |
My use-case is I'm sending a callback to a library which has the wrong type specialization. I also don't want to have to remember this spot in the code in case these variables ever change their type. So it looks like
I do use
|
Have you tried |
TS2304: Cannot find name 'this'. 243 selectedPlanFilters? = [] as typeof this["filters"]; |
I get no errors for this in VS Code but from the Angular compiler. Did anyone has a workaround for TS2304? I would like to have FormGroup.value typed in ngSubmit function with |
More or less the same, it used to work while on Angular 14 (also tried with Angular 15), but with Angular 16 it compiles, then after a save it recompiles partially and this error pops up. |
Is an explicit type an option? It seems like a cleaner version. For example, with TypeScript utility type Record (Repl): type SomeType = Record<string, string>;
// ^? type SomeType = { [x: string]: string; }
class Test {
static data1: SomeType = {};
// ^? (property) Test.data1: SomeType
data2: SomeType = {};
// ^? (property) Test.data2: SomeType
constructor() {
let copy1: SomeType = {};
// ^? let copy1: SomeType
let copy2: typeof Test.data1 = {};
// ^? let copy2: SomeType
let copy3: typeof this.data2 = {};
// ^? let copy3: SomeType
}
}
|
Is this by design or a bug?
The text was updated successfully, but these errors were encountered: