https://github.com/igelbox/lectures
Some examples for my lectures
https://github.com/igelbox/lectures
Last synced: 10 months ago
JSON representation
Some examples for my lectures
- Host: GitHub
- URL: https://github.com/igelbox/lectures
- Owner: igelbox
- Created: 2017-09-24T00:42:57.000Z (over 8 years ago)
- Default Branch: ts-js
- Last Pushed: 2017-10-02T08:43:07.000Z (over 8 years ago)
- Last Synced: 2025-03-06T13:47:25.858Z (over 1 year ago)
- Language: TypeScript
- Size: 12.7 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### Advanced examples for a lecture about integration TypeScript into a JavaScript-based project.
There are two complex examples which require a special/custom version of TypeScript compiler:
1. The `dynamic_cast` example:
```ts
type Struct = {
a: number,
b: string[],
c: 'abc' | 'def',
}
const value: any = {
a: 123,
b: [],
c: 'qwe',
};
const cast = dynamic_cast(value); // Will fail with 'should be equal to one of the allowed values' error
```
2. The `Spring-like REST controller` example:
```ts
@RestController({ path: '/api/v1' })
class TestController {
@RequestMapping({ path: '/{pathVariable}', method: 'POST' })
request(
@RequestBody() body: {
a: number;
b: string;
c: number[];
},
@PathVariable({}) pathVariable: number,
@RequestParam() flag: boolean,
) {
return {
body,
path: pathVariable,
flag,
};
}
}
```
So, the following request will fail due to all three parameters (path variable, query parameter, and request body) have incompatible types.
```js
POST /api/v1/qwe?flag=rty
{
a: 1,
b: 'two',
c: [1, '2']
}
```