When using versions and letting npm install dedupe the packages, npm
audit does not work and it exits with a cryptic error:
The server said: Invalid package tree, run npm install to rebuild your package-lock.json
package.jsons are changed because dependencies are now sorted
tsconfig.jsons are changed because all referenced monorepo packages will
be added as project references
tsconfig.esm.jsons are changed because syncEsmConfig was run
This is to help guard against types that can be undefined since the
recommended eslint rules recommend against using `!` operator on values
like:
const obj: Obj | undefined = ...
obj!.value
Now we can use:
const obj: Obj | undefined = ...
const obj2: Obj = guard(isDefined, obj)
obj.value
The guard function will throw an error if obj parameter is undefined.
As seen in TeamService2.ts, the Contextual type will add Context as the
last argument to any method of the interface, as long as the method has
0-4 arguments.
Interfaces with more than 4 arguments cannot use this type, but they
could be converted to interfaces which use 1 argument
(object/dictionary) only.
Some long-term thinking: Maybe only methods with a single argument
should be supported, similar to the way gRPC does it.
A few notes:
1) The context higher-order function will be tedious to define - a lot
more typing than just an extra function argument.
2) As some methods do not require the context function, forgetting to
call it might introduce bugs (await fn will not error out), but
compile checks of the return value type might detect this
Possible solution is to go the GRPC way and allow only a single method
type as a parameter so all server-side method types would look like:
async fn(param: IParam, context: Context) {}
and all client-side method types would look like:
async fn(param: IParam) {}
However, This would deviate from the JSON-RPC standard which allows
multiple arguments to be passed in an array.
Alternatively, context could be passed as a first argument and then
filtered out:
type Arguments<T> = T extends (context: Ctx, ...args: infer A) => infer R
? A
: never
In this case, the type of Ctx would need to be known in advance. Will
have to think about this some more.
TypeScript compiler needs to be configured to output ES6 module by
setting --module es6 or the equivalent tsconfig.json compilerOptions
parameter.
Since tsc --build does not accept compiler options flags, we need to
duplicate some of the configuration:
- specify separate output folder for ES6 module files (new
tsconfig.esm.json file)
- add "module" field for ES6 module (esm) output files to package.json
Hence, a script `scripts/sync-esm-config.js` was added to automate this
process.