GithubHelp home page GithubHelp logo

Functions from Ramda about fxts HOT 7 CLOSED

gabriel-ss avatar gabriel-ss commented on June 17, 2024 1
Functions from Ramda

from fxts.

Comments (7)

gabriel-ss avatar gabriel-ss commented on June 17, 2024 1

@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.

gabriel-ss avatar gabriel-ss commented on June 17, 2024 1

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.

ppeeou avatar ppeeou commented on June 17, 2024

@gabriel-ss Thanks for your interest, we will review it :)

from fxts.

ppeeou avatar ppeeou commented on June 17, 2024

@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.

ppeeou avatar ppeeou commented on June 17, 2024

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.

ppeeou avatar ppeeou commented on June 17, 2024

oh... you are right. your pr looks good better

from fxts.

ppeeou avatar ppeeou commented on June 17, 2024

release https://github.com/marpple/FxTS/releases/tag/v0.15.0

from fxts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.