mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
* Update react-dependent tests for TS 3.7
Typescript 3.7 includes a flag that will allow people to migrate to the
currently-specified Class Fields ECMA proposal, which is currently at
Stage 3. When `--useDefineForClassFields` is turned on, Typescript
issues 3 new errors in places where the current Typescript semantics
would cause errors with the Stage 3 spec.
Two of the errors are very rare. The third is unfortunately common in
React code because its types don't allow the type of `context` to be
inferred. Instead, components redeclare `context` with an explicit type:
```ts
class ColumnSizerExample extends React.Component<any, any> {
context: React.ContextType<typeof MyContext>
}
```
Without `--useDefineForClassFields`, this *only* redeclares the type of
`context`. With `--useDefineForClassFields`, it redeclares the type of
`context` **and** initialises it to `undefined`. This is very
surprising.
To avoid this, Typescript 3.7 introduces new syntax for exactly this
scenario:
```ts
class ColumnSizerExample extends React.Component<any, any> {
declare context: React.ContextType<typeof MyContext>
}
```
However, Definitely Typed tests cannot use this new syntax because it
only works with Typescript 3.7, which isn't even in beta until next
week. So this PR uses two other workarounds instead:
1. Deleting the declaration when it has the same type as the base,
usually `any`. In this case it's redundant.
2. Using a dummy initialiser:
```ts
class ColumnSizerExample extends React.Component<any, any> {
context = {} as React.ContextType<typeof MyContext>
}
```
This is unfortunate, since it serves as a bad example to anyone reading
the tests. But I couldn't find any other way that works in all
Typescript configurations. I did, however, update the JSDoc for
`Component.context` with instructions to use the new syntax in TS 3.7
and above.
* Disable selected lint rules
* Add missing semicolon
|
||
|---|---|---|
| .. | ||
| index.d.ts | ||
| package.json | ||
| react-native-zss-rich-text-editor-tests.tsx | ||
| tsconfig.json | ||
| tslint.json | ||