#When defining an interface what's the difference between a method signature and function property?

1 messages · Page 1 of 1 (latest)

primal pebble
#

I noticed something odd where when I define a method signature (i.e. foo(name: string): string) I can write code that compiles but will cause a runtime error, but if it's a function property (i.e. foo: (name: string) => string) it works as expected. Can someone explain why that is?

Specifically, for some reason when Foo implements IFoo it can use a different argument type when using a method signature. This leads to a runtime error, yet the type checker is fine.

TSPlayground link: https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgJIDED2nkG8BQyRyAJpgCoAWoA5gBQC2mJEANgFzICCUNAlJwDOYKLQDc+AL758CVnEGDkWHMAYAHVhAYRwSjNjyFi6gK4AjVsASkK1EPSYsOyACLRgANwgke-ISK0RsQhyFAQYKZQIMhObAB0LKLeJBIh0tL4oJCwiCh+wcTmChABog4SmdnQ8EhuHikFEAAekCAkSgUEIUlePmXiUjIImCDCyDDYnAY4ALzIIBAA7srYdHwSsqOCmFrxrJj0k5iJdrR0uMjFgqXIAETXEHfIknwb+EA

sonic lionBOT
#

@primal pebble Here's a shortened URL of your playground link! You can remove the full link from your message.

akajb#0

Preview:```ts
interface IFoo {
doThing(model: Arg): string
}

class Foo implements IFoo {
public doThing(model: DerivedArg): string {
return model.derived
}
}

interface Arg {
base: string
}

interface DerivedArg extends Arg {
derived: string
...```