Comments (7)
@ppeeou indeed pluck would work in the example I gave. I chose it for its simplicity but perhaps it wasn't the best choice; the places where these two show up in our codebase are closer to something like:
type Item = string
const defaultItemPage = { itemsPage: [] as Item[], currentPage: 1, totalOfPages: 1 }
type ItemPage = typeof defaultItemPage;
const fetchItemsPage = (pageNumber: number) => {
const response = fetch(`/itemsPage/${pageNumber}`).then(r => r.json()).catch(always(defaultItemPage))
itemsProxy.itemsPage = response.then(prop("itemsPage"))
itemsProxy.currentPage = response.then(prop("currentPage"))
itemsProxy.totalOfPages = response.then(prop("totalOfPages"))
};
I see that you opened #201; thanks for taking the time to address this issue! I opened #202 adding some other functions, but I also included an implementation suggestion of prop
because type inference on #201 failed for me in some cases. More specifically
const objs = [{ key: "value" } as const, { key: "not value" } as const, null];
const obj1 = prop("key", { key: "value" } as const);
const obj2 = prop("key")({ key: "value" } as const);
const obj3 = prop("key", null);
const obj4 = prop("key")(null);
const obj5 = prop("key", objs[1]);
const obj6 = prop("key")(objs[1]);
makes typescript complain of objects from 2 to 6. Feel free to use or modify it if you want!
from fxts.
4.9.5 from the environment too; more precisely, with
gabriel@desktop ~/Projects/fxts (git)-[main] % npm exec tsc -- --version
Version 4.9.5
and
/**
*
* Returns a function that when supplied an object returns the indicated property of that object, if it exists.
*
* @example
* ```ts
* const arr = [1, 2, 3, 4, 5];
* const n = prop(0, arr); // number
*
* const obj = {a: "1", b: 2};
* const s = prop('a', obj); // string
* const n = prop('b', obj); // number
*
* pipe(
* [{name:"foo", value: "bar"}, {name:"foo2", value: "bar2"}],
* map(prop('name')),
* toArray,
* ); // ["foo", "foo2"]
* ```
*/
export function prop<O extends object, K extends keyof O>(
a: K,
): (obj: O) => O[K];
export function prop<O extends object, K extends keyof O>(a: K, obj: O): O[K];
export function prop<O extends object, K extends keyof O>(a: K, obj?: O): any {
if (obj === undefined) {
return (obj2: O): O[K] => {
return obj2[a];
};
}
return obj[a];
}
export default prop;
const objs = [{ key: "value" } as const, { key: "not value" } as const, null];
const obj1 = prop("key", { key: "value" } as const);
const obj2 = prop("key")({ key: "value" } as const);
const obj3 = prop("key", null);
const obj4 = prop("key")(null);
const obj5 = prop("key", objs[1]);
const obj6 = prop("key")(objs[1]);
I get
gabriel@desktop ~/Projects/fxts (git)-[main] % npm run compile:check
> @fxts/[email protected] compile:check
> tsc --project tsconfig.json
src/prop.ts:42:19 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
42 const obj2 = prop("key")({ key: "value" } as const);
~~~~~
src/prop.ts:43:19 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
43 const obj3 = prop("key", null);
~~~~~
src/prop.ts:44:19 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
44 const obj4 = prop("key")(null);
~~~~~
src/prop.ts:44:26 - error TS2345: Argument of type 'null' is not assignable to parameter of type 'object'.
44 const obj4 = prop("key")(null);
~~~~
src/prop.ts:45:19 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
45 const obj5 = prop("key", objs[1]);
~~~~~
src/prop.ts:46:19 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
46 const obj6 = prop("key")(objs[1]);
~~~~~
src/prop.ts:46:26 - error TS2345: Argument of type '{ readonly key: "value"; } | { readonly key: "not value"; } | null' is not assignable to parameter of type 'object'.
Type 'null' is not assignable to type 'object'.
46 const obj6 = prop("key")(objs[1]);
~~~~~~~
Found 7 errors in the same file, starting at: src/prop.ts:42
from fxts.
@gabriel-ss Thanks for your interest, we will review it :)
from fxts.
@gabriel-ss we will support prop
but you can use this https://fxts.dev/docs/pluck before
it might be the feature you want
from fxts.
Can you tell me what version of typescript it complained about?
In my current environment (fxts, typescript 4.9.5) this seems to be reasonably inferred.
const { checks, check } = Test;
const objs = [{ key: "value" } as const, { key: "not value" } as const, null];
const obj1 = prop("key", { key: "value" } as const);
const obj2 = prop("key")({ key: "value" } as const);
const obj3 = prop("key", null);
const obj4 = prop("key")(null);
const obj5 = prop("key", objs[1]);
const obj6 = prop("key")(objs[1]);
checks([
check<typeof obj1, "value", Test.Pass>(),
check<typeof obj2, "value", Test.Pass>(),
check<typeof obj3, undefined, Test.Pass>(),
check<typeof obj4, undefined, Test.Pass>(),
check<typeof obj5, "value" | "not value" | undefined, Test.Pass>(),
check<typeof obj6, "value" | "not value" | undefined, Test.Pass>(),
]);
from fxts.
oh... you are right. your pr looks good better
from fxts.
release https://github.com/marpple/FxTS/releases/tag/v0.15.0
from fxts.
Related Issues (20)
- feat: enhance entries type HOT 1
- docs: provide recipe for beginner HOT 1
- Type inference is not working when used with Enum HOT 1
- "Type instantiation is excessively deep and possibly infinite" error with large Enum values. HOT 1
- Change `reduce` behavior, When there are two arguments for `reduce`, and the first iterator value is done:true
- docs: support seo HOT 3
- Missing type of `reduce` with initial seed in declarative form HOT 7
- Better documentation and parameter name for `reduce` HOT 2
- Better type declaration for `prop` function HOT 3
- Test of `flat.spec.ts` is broken in `main` branch HOT 2
- Improve readability of package.json
- feat: support fluent Interface
- feat: immutable sort HOT 1
- fxts flatMap causes 'Maximum call stack size exceeded' HOT 1
- docs: Typing Error on fx doc
- How to get string type on pipe? HOT 2
- bug: `flat` type not returned properly in `fx`
- docs: broken links in Guide/Lazy Evaluation document
- feat: `fx` is also applied to operate as iterable by default.
- Add order comment in uniq, uniqBy document
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 fxts.