-
Notifications
You must be signed in to change notification settings - Fork 12.8k
'Property does not exist on type 'never'." after updating to TS 2.0 #12083
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
Comments
Don't use empty classes. |
Why is class A {}
class B extends A {}
class C extends A {}
function foo<T extends A>(t: T): void {
if (!(t instanceof B)) {
// t has type never here
alert(t.constructor.name + ' is not a B');
}
}
foo(new B()); // No alert displayed
foo(new C()); // Alert displayed Irrespective of whether or not |
An additional element here is that the compiler narrows unions differently to how it narrows non-unions. For example narrowing a union of two structurally identical types works as expected (fixed by #10216): class Base {stuff:any}
class A {}
class B {}
function foo(x: A|B) {
if (x instanceof A) {
x // x is A here
}
else {
x // x is B here
}
} But the OP example doesn't narrow from a union type, it narrows from a base class, which doesn't perform narrowing the same way: function bar(x: Base) {
if (x instanceof A) {
x // x is Base here
}
else {
x // x is never here
}
} Could you change your code to use a union type instead of a base class? Otherwise, it's a question for whether non-union type narrowing could be improved. |
Thanks for the tip on the properties. I changed my workaround to use artificial properties. |
I'm seeing the same "does not exist on type 'never'" error when using a union type (unlike the OP):
Adding a dummy property to
... but is a hack. It's not entirely clear to me from looking at the referenced issues here whether this is already tracked as a bug; I assume it should be. Is this already tracked? |
this should work: if (action instanceof SetSymbolAction) {
..
} else if (action instanceof SetFilingAction) {
..
} else if (action instanceof ClearTickersAction) {
..
} |
@jamesandersen probably #202, since TS would need nominal |
the reson is the narrowing removes constitents from the type. |
@mhegazy interesting... that is a cleaner approach; at least until there's another |
After updating https://github.com/Microsoft/vscode-css-languageservice to TS 2.0, I get a lot of errors about accesses to a type never.
They always occur in happen after
instanceof
checksMore workaround for the same error:
cssParser.ts#L135
cssCompletion.ts#L50
cssHover.ts#L41
selectorPrinting.ts#L311
selectorPrinting.ts#L311
The text was updated successfully, but these errors were encountered: