Answer the question
In order to leave comments, you need to log in
Function parameter typechecking bug?
The code:
type AB = {
a: number
b: string
}
type A = {
a: number
}
interface Int1 {
f(p: A): void
// f: (p: A) => void
}
class Class1 implements Int1 {
f(p: AB): void { console.log(p) }
// f = (p: AB) => { console.log(p) }
}
var o: Int1 = new Class1();
o.f({a: 1})
Answer the question
In order to leave comments, you need to log in
Great question.
Strict function types :
The stricter checking applies to all function types, except those originating in method or constructor declarations. Methods are excluded specifically to ensure generic classes and interfaces (such as Array) continue to mostly relate covariantly.
type AFnc = (p: A) => void
function foo(p: A): void { }
function bar(p: AB): void { }
const f: AFnc = foo;
const b: AFnc = bar; // не компилируется с сообщением о том, что AB требует ещё и свойство b
Because AB is a special case of A.
Roughly speaking, the typescript compares not the names of interfaces, but their properties and their types. That is, you can pass in any type that is a special case of the type you want.
If you change the property type in AB а
to string
, then the error will immediately appear.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question