Comments (3)
The optional annotation is also used as an element of isTupleOf
and isParametersOf
, so it is impossible to apply pattern 1 of annotating object keys.
from unknownutil.
Several pattern to annotate fields as ?
or readonly
.
import { is, Predicate } from "./mod.ts";
function optional<K extends PropertyKey>(name: K): K {
return name;
}
function readonly<K extends PropertyKey>(name: K): K {
return name;
}
function annotate<T>(
pred: Predicate<T>,
annotations: { optional?: boolean; readonly?: boolean },
): Predicate<T> {
return pred;
}
const as = {
Optional: <T>(pred: Predicate<T>) => pred,
Readonly: <T>(pred: Predicate<T>) => pred,
};
const pattern1 = is.ObjectOf({
[optional("optionalField")]: is.String,
[readonly("readonlyField")]: is.String,
[readonly(optional("readonlyOptionalField"))]: is.String,
});
const pattern2 = is.ObjectOf({
optionalField: annotate(is.String, { optional: true }),
readonlyField: annotate(is.String, { readonly: true }),
readonlyOptionalField: annotate(is.String, {
optional: true,
readonly: true,
}),
});
const pattern3 = is.ObjectOf({
optionalField: as.Optional(is.String),
readonlyField: as.Readonly(is.String),
readonlyOptionalField: as.Readonly(as.Optional(is.String)),
});
I personally like as.*
pattern
from unknownutil.
What I'm thinking of it is
as.Optional
- Optional for a field or an elementas.Readonly
- Readonly for a field or an elementis.PartialOf
- Optional for all fields or elements of object, array, tuple, etc.is.ReadonlyOf
- Readonly for all fields or elements of object, array, tuple, etc.
type A = {
foo?: string;
readonly bar: string;
readonly hoge?: string;
};
// Predicate<A>
const a = is.ObjectOf({
foo: as.Optional(is.String),
bar: as.Readonly(is.String),
hoge: as.Optional(as.Readonly(is.String)),
});
type B = Partial<A>;
// Predicate<B>
const b = is.PartialOf(a);
type C = Readonly<A>;
// Predicate<C>
const c = is.ReadonlyOf(a);
from unknownutil.
Related Issues (12)
- Switch to `dnt` to provide node package HOT 1
- CI for npm publish does not work HOT 2
- Predicate function generated by `isOneOf<T>` narrowed to invalid type in some case
- Add `OmitOf` and `PickOf` to modify object predicate function HOT 2
- Add `isMap` and `isMapOf` like `isRecord` and `isRecordOf`
- Add `isSet` and `isSetOf` like `isArray` and `isArrayOf`
- Record containing key `constructor` cannot be handled by RecordOf. HOT 2
- Refine `isRecordLike` and `isRecord`
- Better error message on `assert` and `ensure` HOT 1
- Type mismatch on `intersection` of `object` and `union`
- Remove `isXXXXX` and provide only `is.XXXXX` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unknownutil.