You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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...
Reacted by xahon, pizzarob, maxiemyzer, Mingwei Samuel, Jochem Klaver, Jorge Fuentes, François Levasseur, João Vitor, ExE Boss, Tobias Berger and 5 more
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 this to anything other than that, it would be fine and no error would be reported. e.g.
I think the reason it isn't allowed is that something like this
functionfoo(){varx: typeofthis.x;}
is perfectly valid, because this more or less has type any. So while it might make sense in a class, it doesn't make sense in any other context unless we had #229.
Ryan Cavanaugh (@RyanCavanaugh), but is it appropriate to allow it in the context I gave? It will inevitably lead to people doing so and getting any as their type.
We already let you write typeof foo.bar where foo is of type any, and we already let you write this.x in other dotted-expression positions when the type of this is any. I don't see why their intersection at typeof this.x should merit an exception.
Reacted by sam-s4s, Max Coplan and Qwerty (Vítězslav Ackermann Ferko)
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:
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.
In this case it is not appropriate to just change thing to a static variable and say typeof ClassName.thing because it will be different in each instance. Also multi needs to be a class variable, and thus must have its type in class declaration time.
sam-s4s nowdays you aught to be able to write CMultiDataset<this["thing"]>, instead. Indexed acceses do work with this (a polymorphic one, even), unlike typeof.
sam-s4s nowdays you aught to be able to write CMultiDataset<this["thing"]>, instead. Indexed acceses do work with this (a polymorphic one, even), unlike typeof.
I have just tried, but it doesn't work in TypeScript 3.4.4 though, sam-s4s 's one works (ClassName['thing']). I think it might make more sense that this should work, instead of only able to use the "ClassName"?
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
async foundBob(playerTypeLess: any, bobTypeLess: any) {
const player = playerTypeLess as typeof this.player;
^^ ts(2304) Cannot find name 'this'.
I do use this later in that same method. So the error message is a bit misleading. I end up doing the simpler but more fragile:
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 typeof this.formGroup.value.
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 typeof this.formGroup.value.
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.
Activity
giancarloa commentedon Dec 24, 2014
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...
vilicvane commentedon Dec 24, 2014
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
thisto anything other than that, it would be fine and no error would be reported. e.g.DanielRosenwasser commentedon Dec 26, 2014
As per the spec
So this is expected.
I think the reason it isn't allowed is that something like this
is perfectly valid, because
thismore or less has typeany. So while it might make sense in a class, it doesn't make sense in any other context unless we had #229.vilicvane commentedon Dec 26, 2014
Daniel Rosenwasser (@DanielRosenwasser) O.O hmm... then hoping you would consider updating the spec. lol.
DanielRosenwasser commentedon Dec 27, 2014
It's something to consider; if your members are not private, you can easily get around this with the following in the mean time.
NN--- commentedon Dec 29, 2014
Waiting for this feature.
I think the simplest case should be supported
var a : typeof this.x = 1;
And a recursive typeof member should lead to error.
Thanks.
RyanCavanaugh commentedon Dec 30, 2014
Seems like this should just work if we changed the grammar.
DanielRosenwasser commentedon Dec 30, 2014
Ryan Cavanaugh (@RyanCavanaugh), but is it appropriate to allow it in the context I gave? It will inevitably lead to people doing so and getting
anyas their type.vilicvane commentedon Dec 30, 2014
syntactically correct doesn't have to mean that it should actually make sense, IMO.
a warning could be raised for this cause, though.
RyanCavanaugh commentedon Dec 30, 2014
We already let you write
typeof foo.barwherefoois of typeany, and we already let you writethis.xin other dotted-expression positions when the type ofthisisany. I don't see why their intersection attypeof this.xshould merit an exception.juanevp commentedon Feb 3, 2015
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:
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.
mwisnicki commentedon Mar 29, 2015
Juan Vidal Pich (@juanevp) How about function interface ?
6 remaining items
sam-s4s commentedon Feb 5, 2018
Hmmm I've just run into this problem :(
I'm defining an object in my class.
Then I'm defining another object that has a type param that needs to be the type of the above.
In this case it is not appropriate to just change thing to a static variable and say
typeof ClassName.thingbecause it will be different in each instance. Alsomultineeds to be a class variable, and thus must have its type in class declaration time.Ryan Cavanaugh (@RyanCavanaugh) Daniel Rosenwasser (@DanielRosenwasser) Mohamed Hegazy (@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?
weswigham commentedon Feb 5, 2018
sam-s4s nowdays you aught to be able to write
CMultiDataset<this["thing"]>, instead. Indexed acceses do work withthis(a polymorphic one, even), unliketypeof.sam-s4s commentedon Feb 5, 2018
Thanks Wesley Wigham (@weswigham) - I did find out that I can do
CMultiDataset<ClassName['thing']>which mostly gets me out of trouble :)I could not find any combination using
thisthat would work though...yxliang01 commentedon Jul 17, 2019
I have just tried, but it doesn't work in TypeScript 3.4.4 though, sam-s4s 's one works (
ClassName['thing']). I think it might make more sense thatthisshould work, instead of only able to use the "ClassName"?vegerot commentedon Jun 8, 2020
If I have something like
What is the proposed workaround? And I cannot make
groupByTypesstaticFeofilakt commentedon Apr 6, 2021
OMG, 7 years old design bug. Ok, see what would be after yet another 7.
ubershmekel commentedon Apr 25, 2021
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
thislater in that same method. So the error message is a bit misleading. I end up doing the simpler but more fragile:Zzzen commentedon Apr 30, 2021
Have you tried
const player = playerTypeLess as this['player']? Yuval Greenfield (@ubershmekel)royeradames commentedon Jan 13, 2022
TS2304: Cannot find name 'this'.
243 selectedPlanFilters? = [] as typeof this["filters"];
muuvmuuv commentedon Oct 1, 2023
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
typeof this.formGroup.value.Xriuk commentedon Apr 17, 2024
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.
serious-angel commentedon May 6, 2024
Is an explicit type an option? It seems like a cleaner version. For example, with TypeScript utility type Record (Repl):