Skip to content

Commit ba07c92

Browse files
authored
Update README.md
1 parent e267190 commit ba07c92

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,21 @@ calculator.received().add(1, Arg.any());
2828
calculator.didNotReceive().add(2, 2);
2929
```
3030

31-
## Why require TypeScript 3?
31+
### Creating a mock
32+
`var myFakeCalculator = Substitute.for<Calculator>();`
33+
34+
### Setting return types
35+
Multiple return types can be set in sequence. Consider the example below.
36+
37+
```
38+
myFakeCalculator.add(1, 2).returns(3, 7, 9);
39+
console.log(myFakeCalculator.add(1, 2)); //prints 3
40+
console.log(myFakeCalculator.add(1, 2)); //prints 7
41+
console.log(myFakeCalculator.add(1, 2)); //prints 9
42+
console.log(myFakeCalculator.add(1, 2)); //prints undefined
43+
```
44+
45+
## Why TypeScript 3?
3246
Let's say I want to mock the type T, so I create a function `function mock<T>(): T`. If I then passed the object `{ doFooWithOneArgument: (input: string) => boolean, doFooWithTwoArguments: (input1: string, input2: boolean) => string }` to it, I would get a strong-typed variant of it in return.
3347

3448
That's all good, but if I wanted to map that type into `{ doFooWithOneArgument: (whateverTheOneParameterTypeWas) => boolean, doWithTwoArguments: (whateverTheTwoParameterTypesWas) => string } & { returns: (...returnValuesInSequence: whateverTheReturnTypeOfDoFooWas[]) => void }`, I wouldn't be able to do it in a fully strong-typed manner, since there would be no way of expressing `whateverTheOneParameterTypeWas` and `whateverTheTwoParameterTypesWas` in a mapped type, and they would have to be defined as `...args: any[]`. However, now there's "Generic rest parameters" that can express this, as seen in [the example in the announcement post](https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#generic-rest-parameters).

0 commit comments

Comments
 (0)