mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-07 05:11:16 -06:00
Merge pull request #15320 from Microsoft/fixTypePredicateThisParameter
Fix type predicates with this parameters
This commit is contained in:
commit
a0abadbcbe
@ -11565,7 +11565,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
if (isIdentifierTypePredicate(predicate)) {
|
||||
const predicateArgument = callExpression.arguments[predicate.parameterIndex];
|
||||
const predicateArgument = callExpression.arguments[predicate.parameterIndex - (signature.thisParameter ? 1 : 0)];
|
||||
if (predicateArgument) {
|
||||
if (isMatchingReference(reference, predicateArgument)) {
|
||||
return getNarrowedType(type, predicate.type, assumeTrue, isTypeSubtypeOf);
|
||||
|
||||
43
tests/baselines/reference/typePredicateWithThisParameter.js
Normal file
43
tests/baselines/reference/typePredicateWithThisParameter.js
Normal file
@ -0,0 +1,43 @@
|
||||
//// [typePredicateWithThisParameter.ts]
|
||||
// Repro from #15310
|
||||
|
||||
interface Foo {
|
||||
foo: string;
|
||||
}
|
||||
interface Bar {
|
||||
bar: string;
|
||||
}
|
||||
|
||||
function isFoo1(object: {}): object is Foo {
|
||||
return 'foo' in object;
|
||||
}
|
||||
|
||||
function isFoo2(this: void, object: {}): object is Foo {
|
||||
return 'foo' in object;
|
||||
}
|
||||
|
||||
declare let test: Foo | Bar;
|
||||
|
||||
if (isFoo1(test)) {
|
||||
test.foo = 'hi';
|
||||
}
|
||||
|
||||
if (isFoo2(test)) {
|
||||
test.foo = 'hi';
|
||||
}
|
||||
|
||||
|
||||
//// [typePredicateWithThisParameter.js]
|
||||
// Repro from #15310
|
||||
function isFoo1(object) {
|
||||
return 'foo' in object;
|
||||
}
|
||||
function isFoo2(object) {
|
||||
return 'foo' in object;
|
||||
}
|
||||
if (isFoo1(test)) {
|
||||
test.foo = 'hi';
|
||||
}
|
||||
if (isFoo2(test)) {
|
||||
test.foo = 'hi';
|
||||
}
|
||||
@ -0,0 +1,62 @@
|
||||
=== tests/cases/compiler/typePredicateWithThisParameter.ts ===
|
||||
// Repro from #15310
|
||||
|
||||
interface Foo {
|
||||
>Foo : Symbol(Foo, Decl(typePredicateWithThisParameter.ts, 0, 0))
|
||||
|
||||
foo: string;
|
||||
>foo : Symbol(Foo.foo, Decl(typePredicateWithThisParameter.ts, 2, 15))
|
||||
}
|
||||
interface Bar {
|
||||
>Bar : Symbol(Bar, Decl(typePredicateWithThisParameter.ts, 4, 1))
|
||||
|
||||
bar: string;
|
||||
>bar : Symbol(Bar.bar, Decl(typePredicateWithThisParameter.ts, 5, 15))
|
||||
}
|
||||
|
||||
function isFoo1(object: {}): object is Foo {
|
||||
>isFoo1 : Symbol(isFoo1, Decl(typePredicateWithThisParameter.ts, 7, 1))
|
||||
>object : Symbol(object, Decl(typePredicateWithThisParameter.ts, 9, 16))
|
||||
>object : Symbol(object, Decl(typePredicateWithThisParameter.ts, 9, 16))
|
||||
>Foo : Symbol(Foo, Decl(typePredicateWithThisParameter.ts, 0, 0))
|
||||
|
||||
return 'foo' in object;
|
||||
>object : Symbol(object, Decl(typePredicateWithThisParameter.ts, 9, 16))
|
||||
}
|
||||
|
||||
function isFoo2(this: void, object: {}): object is Foo {
|
||||
>isFoo2 : Symbol(isFoo2, Decl(typePredicateWithThisParameter.ts, 11, 1))
|
||||
>this : Symbol(this, Decl(typePredicateWithThisParameter.ts, 13, 16))
|
||||
>object : Symbol(object, Decl(typePredicateWithThisParameter.ts, 13, 27))
|
||||
>object : Symbol(object, Decl(typePredicateWithThisParameter.ts, 13, 27))
|
||||
>Foo : Symbol(Foo, Decl(typePredicateWithThisParameter.ts, 0, 0))
|
||||
|
||||
return 'foo' in object;
|
||||
>object : Symbol(object, Decl(typePredicateWithThisParameter.ts, 13, 27))
|
||||
}
|
||||
|
||||
declare let test: Foo | Bar;
|
||||
>test : Symbol(test, Decl(typePredicateWithThisParameter.ts, 17, 11))
|
||||
>Foo : Symbol(Foo, Decl(typePredicateWithThisParameter.ts, 0, 0))
|
||||
>Bar : Symbol(Bar, Decl(typePredicateWithThisParameter.ts, 4, 1))
|
||||
|
||||
if (isFoo1(test)) {
|
||||
>isFoo1 : Symbol(isFoo1, Decl(typePredicateWithThisParameter.ts, 7, 1))
|
||||
>test : Symbol(test, Decl(typePredicateWithThisParameter.ts, 17, 11))
|
||||
|
||||
test.foo = 'hi';
|
||||
>test.foo : Symbol(Foo.foo, Decl(typePredicateWithThisParameter.ts, 2, 15))
|
||||
>test : Symbol(test, Decl(typePredicateWithThisParameter.ts, 17, 11))
|
||||
>foo : Symbol(Foo.foo, Decl(typePredicateWithThisParameter.ts, 2, 15))
|
||||
}
|
||||
|
||||
if (isFoo2(test)) {
|
||||
>isFoo2 : Symbol(isFoo2, Decl(typePredicateWithThisParameter.ts, 11, 1))
|
||||
>test : Symbol(test, Decl(typePredicateWithThisParameter.ts, 17, 11))
|
||||
|
||||
test.foo = 'hi';
|
||||
>test.foo : Symbol(Foo.foo, Decl(typePredicateWithThisParameter.ts, 2, 15))
|
||||
>test : Symbol(test, Decl(typePredicateWithThisParameter.ts, 17, 11))
|
||||
>foo : Symbol(Foo.foo, Decl(typePredicateWithThisParameter.ts, 2, 15))
|
||||
}
|
||||
|
||||
@ -0,0 +1,72 @@
|
||||
=== tests/cases/compiler/typePredicateWithThisParameter.ts ===
|
||||
// Repro from #15310
|
||||
|
||||
interface Foo {
|
||||
>Foo : Foo
|
||||
|
||||
foo: string;
|
||||
>foo : string
|
||||
}
|
||||
interface Bar {
|
||||
>Bar : Bar
|
||||
|
||||
bar: string;
|
||||
>bar : string
|
||||
}
|
||||
|
||||
function isFoo1(object: {}): object is Foo {
|
||||
>isFoo1 : (object: {}) => object is Foo
|
||||
>object : {}
|
||||
>object : any
|
||||
>Foo : Foo
|
||||
|
||||
return 'foo' in object;
|
||||
>'foo' in object : boolean
|
||||
>'foo' : "foo"
|
||||
>object : {}
|
||||
}
|
||||
|
||||
function isFoo2(this: void, object: {}): object is Foo {
|
||||
>isFoo2 : (this: void, object: {}) => object is Foo
|
||||
>this : void
|
||||
>object : {}
|
||||
>object : any
|
||||
>Foo : Foo
|
||||
|
||||
return 'foo' in object;
|
||||
>'foo' in object : boolean
|
||||
>'foo' : "foo"
|
||||
>object : {}
|
||||
}
|
||||
|
||||
declare let test: Foo | Bar;
|
||||
>test : Foo | Bar
|
||||
>Foo : Foo
|
||||
>Bar : Bar
|
||||
|
||||
if (isFoo1(test)) {
|
||||
>isFoo1(test) : boolean
|
||||
>isFoo1 : (object: {}) => object is Foo
|
||||
>test : Foo | Bar
|
||||
|
||||
test.foo = 'hi';
|
||||
>test.foo = 'hi' : "hi"
|
||||
>test.foo : string
|
||||
>test : Foo
|
||||
>foo : string
|
||||
>'hi' : "hi"
|
||||
}
|
||||
|
||||
if (isFoo2(test)) {
|
||||
>isFoo2(test) : boolean
|
||||
>isFoo2 : (this: void, object: {}) => object is Foo
|
||||
>test : Foo | Bar
|
||||
|
||||
test.foo = 'hi';
|
||||
>test.foo = 'hi' : "hi"
|
||||
>test.foo : string
|
||||
>test : Foo
|
||||
>foo : string
|
||||
>'hi' : "hi"
|
||||
}
|
||||
|
||||
26
tests/cases/compiler/typePredicateWithThisParameter.ts
Normal file
26
tests/cases/compiler/typePredicateWithThisParameter.ts
Normal file
@ -0,0 +1,26 @@
|
||||
// Repro from #15310
|
||||
|
||||
interface Foo {
|
||||
foo: string;
|
||||
}
|
||||
interface Bar {
|
||||
bar: string;
|
||||
}
|
||||
|
||||
function isFoo1(object: {}): object is Foo {
|
||||
return 'foo' in object;
|
||||
}
|
||||
|
||||
function isFoo2(this: void, object: {}): object is Foo {
|
||||
return 'foo' in object;
|
||||
}
|
||||
|
||||
declare let test: Foo | Bar;
|
||||
|
||||
if (isFoo1(test)) {
|
||||
test.foo = 'hi';
|
||||
}
|
||||
|
||||
if (isFoo2(test)) {
|
||||
test.foo = 'hi';
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user