Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit f991719

Browse files
author
Orta
authored
Merge pull request #1094 from srcdes/patch-1
Add code sample and clarify some expression
2 parents d2c7360 + 381fa12 commit f991719

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

pages/Interfaces.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ mySearch = function(src: string, sub: string): boolean {
255255
Function parameters are checked one at a time, with the type in each corresponding parameter position checked against each other.
256256
If you do not want to specify types at all, TypeScript's contextual typing can infer the argument types since the function value is assigned directly to a variable of type `SearchFunc`.
257257
Here, also, the return type of our function expression is implied by the values it returns (here `false` and `true`).
258-
Had the function expression returned numbers or strings, the type checker would have warned us that return type doesn't match the return type described in the `SearchFunc` interface.
259258

260259
```ts
261260
let mySearch: SearchFunc;
@@ -265,6 +264,19 @@ mySearch = function(src, sub) {
265264
}
266265
```
267266

267+
Had the function expression returned numbers or strings, the type checker would have made an error that indicates return type doesn't match the return type described in the `SearchFunc` interface.
268+
269+
```ts
270+
let mySearch: SearchFunc;
271+
272+
// error: Type '(src: string, sub: string) => string' is not assignable to type 'SearchFunc'.
273+
// Type 'string' is not assignable to type 'boolean'.
274+
mySearch = function(src, sub) {
275+
let result = src.search(sub);
276+
return "string";
277+
};
278+
```
279+
268280
# Indexable Types
269281

270282
Similarly to how we can use interfaces to describe function types, we can also describe types that we can "index into" like `a[10]`, or `ageMap["daniel"]`.

0 commit comments

Comments
 (0)