Skip to content

Commit 8fdd411

Browse files
authored
Update TypeScript docs in README (#103)
`as const` doesn't always work, so the `AllowedSchema` type from this package must be used.
1 parent e4d7264 commit 8fdd411

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,29 @@ Coming from `express-jsonschema`? Read the [migration notes](docs/migrating-from
7171

7272
### Schemas in TypeScript
7373

74-
If you are writing JSON schemas in TypeScript, you will need to cast your schema
75-
to the `const` type e.g.
74+
If you're writing JSON schemas in TypeScript, you'll need to use the
75+
`AllowedSchema` type e.g.
7676

7777
```typescript
78-
const addressSchema = {
78+
import { AllowedSchema } from "express-json-validator-middleware";
79+
80+
const addressSchema: AllowedSchema = {
7981
type: "object",
8082
required: ["street"],
8183
properties: {
8284
street: {
8385
type: "string",
8486
}
8587
},
86-
} as const;
88+
};
8789
```
8890

89-
This is required so that TypeScript doesn't attempt to widen the types of values
90-
in the schema object. If you omit the `as const` statement TypeScript will raise
91-
a compilation error. The discussion in
92-
[this issue](https://github.com/simonplend/express-json-validator-middleware/issues/39)
93-
provides further background.
91+
This is required so TypeScript doesn't attempt to widen the types of values
92+
in the schema object. If you omit this type, TypeScript will raise an error.
93+
94+
See issues [#39](https://github.com/simonplend/express-json-validator-middleware/issues/39)
95+
and [#102](https://github.com/simonplend/express-json-validator-middleware/issues/102)
96+
for more background.
9497

9598
## Error handling
9699

0 commit comments

Comments
 (0)