Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: autocomplete #11

Merged
merged 4 commits into from
Oct 12, 2021
Merged

feat: autocomplete #11

merged 4 commits into from
Oct 12, 2021

Conversation

DoneDeal0
Copy link
Owner

@DoneDeal0 DoneDeal0 commented Aug 19, 2021

Start autocomplete feature and refactor code. The autocomplete currently displays all the available paths. It should only display direct child nodes (user => user.describe => user.describe.complex / user.describe.simple instead of immediately displaying final paths: user.describe.complex, user.describe.simple).

The pr also needs to fix the Property 'split' does not exist on type 'never' issue on currentKey.split in T function.

Any help welcome.

@DoneDeal0 DoneDeal0 changed the title feat: start autocomplete feat: autocomplete Aug 19, 2021
@DoneDeal0 DoneDeal0 added the enhancement New feature or request label Aug 19, 2021
@ablankenship10
Copy link

Fixed Property 'split' does not exist on type 'never' in PR #12

@DoneDeal0
Copy link
Owner Author

DoneDeal0 commented Aug 21, 2021

Hi @ablankenship10 , thanks for your pr, it fixes the ts issue! However, let's please send all of our commits to the same PR in order to keeps things organized ;).

The latest commit:

  • Adds your TS fix
  • Introduces a useT() hook that return the classic T function. By wrapping T in useT, it allows us to consume Talkr's context and access the current language object, which is necessary to type the key parameter in T and thus provide an autocomplete.

Remaining issues:

  • The autocomplete doesn't work yet. The typing is good, because if we use the same typings in a random function we get a proper autocomplete:
// en.json
{
  "hello": "hello",
  "talkr": {
    "test": "talkr works"
  },
  "company": {
      "employee": {
          "tech": "tech team",
          "sales": "sales team"
      }
  },
}

// .tsx file:
type KeyPrefix<T extends string> = T extends "" ? "" : `.${T}`;

export type KeyPath<T> = (
  T extends object
    ? {
        [K in Exclude<keyof T, symbol>]: `${K}${KeyPrefix<KeyPath<T[K]>>}`;
      }[Exclude<keyof T, symbol>]
    : ""
) extends infer D
  ? Extract<D, string>
  : never;

type Path = KeyPath<typeof en>

  function getString(path: Path) {
    let result: any = obj
    path.split(".").forEach((k) => {
        if (!result[k]) return;
        return (result = result[k]);
    });
    return typeof result === "string" ? result : null
  }
  

export default function App() {
  return (
    <>
      <h1>{getString("company.employee.sales")}</h1>
    </>
  );
}

useT returns a correct schemaLanguage (checked with console.log). So T's parameter key should be correctly typed. Yet, when using it, I receive the error: Argument of type '"hello"' is not assignable to parameter of type '""'.

When hardcoding the object const schemaLanguage = {hello: "hello", etc.}, the autocomplete works! So we're not far from success... But I don't see why it doesn't work yet.

@ablankenship10
Copy link

 export type KeyPath<T> = (
   T extends object
     ? {
         [K in Exclude<keyof T, symbol>]: `${K}${KeyPrefix<KeyPath<T[K]>>}`;
       }[Exclude<keyof T, symbol>]
     : ""
 ) extends infer D
   ? Extract<D, string>
   : never;
 
 

I receive the error: Argument of type '"hello"' is not assignable to parameter of type '""'.

: "" should be : string, as "" means literally blank string and no other value. Thats what I had changed in my PR to fix the currentKey value.

@DoneDeal0
Copy link
Owner Author

Closes #10 The autocomplete now works with hard-coded strings, concatenation or variables. In order to avoid breaking changes and performance issues for huge json files, the autocompletion is optional, and must be configured in a small translate.ts file. I will publish the package on npm soon.

@DoneDeal0 DoneDeal0 merged commit 4efea7b into master Oct 12, 2021
@DoneDeal0 DoneDeal0 deleted the autocomplete branch July 18, 2022 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants