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
var x: any = "foo";
if(typeof x === 'string') {
console.log(x.subtr(1)); // Error, 'subtr' does not exist on 'string'
}
// x is still any here
x.unknown(); // OK
I am not seeing the error or intellisense for the console.log statement.
Are type guards actually supported in this case or only for union types?
As per conversation off-line yesterday, I think this should continue to give the more specific type. With simple types like 'string' we have a complete picture of what the user should be able to do in the if-block.
Strong agreement that we should narrow expressions of type any when there is a type guard for a primitive type (typeof x === 'string'), but not in other cases (x instanceof foo).
While I understand this for doing "instanceof Object", I cannot think of a usecase where I do "instanceof MyClass" where I would not want an error to show up when I use a property that does not exist?
Also: If I explicitly assign "any" to a type, this is a good indication that I want the type to be any and no errors should show up. But when the type is simply having "any" by default because I did not set a type, you should support type guards.
Benjamin Pasero (@bpasero) - If you're a subclass of MyClass, then instanceof the parent class will be true. This means, we can't say for sure that any property doesn't exist, because it may exist on the subclass.
The net result is that we may error on perfectly valid, and correct, code. We're trying to avoid that with type guards.
I disagree. If I check of instance for a super class of my type, I should
not be allowed to use properties of any class that is below that level,
otherwise I would have checked for that class in the first place.
Typescript should be all about discouraging unsafe, untyped access to an
object.
Am 12.01.2015 18:20 schrieb "Jonathan Turner" notifications@github.com:
Benjamin Pasero (@bpasero)https://github.com/bpasero - If you're a subclass of MyClass,
then instanceof the parent class will be true. This means, we can't say for
sure that any property doesn't exist, because it may exist on the subclass.
The net result is that we may error on perfectly valid, and correct, code.
We're trying to avoid that with type guards.
—
Reply to this email directly or view it on GitHub #1595 (comment)
.
Activity
ahejlsberg commentedon Jan 5, 2015
Type guards do not affect variables of type
any. This is a fairly recent change, see #1426 and #1433.jonathandturner We should probably update the example in the blog post.
sophiajt commentedon Jan 6, 2015
As per conversation off-line yesterday, I think this should continue to give the more specific type. With simple types like 'string' we have a complete picture of what the user should be able to do in the if-block.
RyanCavanaugh commentedon Jan 7, 2015
Strong agreement that we should narrow expressions of type
anywhen there is a type guard for a primitive type (typeof x === 'string'), but not in other cases (x instanceof foo).bpasero commentedon Jan 9, 2015
Thanks!
Am 09.01.2015 00:57 schrieb "Anders Hejlsberg" notifications@github.com:
bpasero commentedon Jan 9, 2015
Btw what is the rationale to not also support this in instanceof blocks?
danquirk commentedon Jan 10, 2015
#1426 has a specific example of the general class of issues with that case.
bpasero commentedon Jan 10, 2015
While I understand this for doing "instanceof Object", I cannot think of a usecase where I do "instanceof MyClass" where I would not want an error to show up when I use a property that does not exist?
bpasero commentedon Jan 10, 2015
Also: If I explicitly assign "any" to a type, this is a good indication that I want the type to be any and no errors should show up. But when the type is simply having "any" by default because I did not set a type, you should support type guards.
sophiajt commentedon Jan 12, 2015
Benjamin Pasero (@bpasero) - If you're a subclass of MyClass, then instanceof the parent class will be true. This means, we can't say for sure that any property doesn't exist, because it may exist on the subclass.
The net result is that we may error on perfectly valid, and correct, code. We're trying to avoid that with type guards.
bpasero commentedon Jan 12, 2015
I disagree. If I check of instance for a super class of my type, I should
not be allowed to use properties of any class that is below that level,
otherwise I would have checked for that class in the first place.
Typescript should be all about discouraging unsafe, untyped access to an
object.
Am 12.01.2015 18:20 schrieb "Jonathan Turner" notifications@github.com: