[IMP] types: do not make Env an indexed type

Before this commit, Env was an indexed type, this means that one could
write env.anything, and it would accept it as a valid type. This is
actually quite dangerous, because we lose the typing advantages for all
keys that are properly defined.

For example, if a component is defined as:

class MyComponent extends Component<Props> {
 ...
}

Then Typescript will let it use anything from the environment, even if
it is wrong.  So, most properly typed Typescript applications should use
instead a sub environment:

interface MyAppEnv extends Env {
  someKey: someValue
}

Then, the component should be defined this way:

class MyComponent extends Component<Props, MyAppEnv> {
 ...
}

Before this commit, any typos in the environment accesses would not be
noticed by typescript.
This commit is contained in:
Géry Debongnie
2020-04-17 11:30:25 +02:00
committed by aab-odoo
parent 94c8bce810
commit 142b69823f
6 changed files with 26 additions and 11 deletions
+5 -2
View File
@@ -3484,11 +3484,14 @@ describe("environment and plugins", () => {
});
test("can define specific env for root components", async () => {
class App1 extends Component {
interface AppEnv extends Env {
test: number;
}
class App1 extends Component<{}, AppEnv> {
static template = xml`<span></span>`;
}
App1.env = { test: 1 };
class App2 extends Component {
class App2 extends Component<{}, AppEnv> {
static template = xml`<span></span>`;
}
App2.env = { test: 2 };
+3 -3
View File
@@ -1,5 +1,5 @@
import { Component, Env } from "../src/component/component";
import { Store, useStore, useDispatch, useGetters } from "../src/store";
import { Store, useStore, useDispatch, useGetters, EnvWithStore } from "../src/store";
import { useState } from "../src/hooks";
import { xml } from "../src/tags";
import { shallowEqual } from "../src/utils";
@@ -885,7 +885,7 @@ describe("connecting a component to store", () => {
actions
});
class TodoItem extends Component {
class TodoItem extends Component<{}, EnvWithStore> {
static template = xml`
<div class="todo">
<t t-esc="state.todo.title"/>
@@ -958,7 +958,7 @@ describe("connecting a component to store", () => {
actions
});
class TodoItem extends Component {
class TodoItem extends Component<{}, EnvWithStore> {
static template = xml`
<div class="todo">
<t t-esc="state.todo.title"/>