The Wayback Machine - https://web.archive.org/web/20200530052544/https://github.com/Yoctol/bottender/issues/630
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

development guide of TypeScript #630

Open
hilezir opened this issue Jan 14, 2020 · 1 comment
Open

development guide of TypeScript #630

hilezir opened this issue Jan 14, 2020 · 1 comment
Assignees
Labels
doc

Comments

@hilezir
Copy link
Contributor

@hilezir hilezir commented Jan 14, 2020

Hi, I just looking for a development guide for TypeScript.

Or maybe just an interface LineClient or LineAction got export is enough for me.

The reason is bottender now got have interfaces of Action, Client, Event and Props etc. It's wonderful.

With these interfaces, I can say const SayHi: Action<Client, Event>, which with argument interfaces context.sendText and props?.next and etc.

But the problem is Action<Client, Event> interface has no context.sendFlex, and seems LineClient has no export by bottender. Therefore I can not tell VSCode Action<LineClient, LineEvent>.

Currently, my workaround is

export const QueryWar3rStreams= async (
 context: LineContext,
  props: Props<Client, LineEvent> & { ownPropsAsString: string },
) => {
  console.info(props.ownPropsAsString)

  return props?.next
}

The workaround is working fine, but it will get the next problem on import { text } from 'bottender'

export default async function App(context: LineContext): Promise<unknown> {
  return router([text(/^[$$](直播|live)/i, QueryWar3rStreams as any)])
}
[ts]   Types of parameters 'context' and 'context' are incompatible.
[ts]     Type 'Context<LineClient, LineEvent>' is missing the following properties from type 'LineContext': _customAccessToken, _isReplied, _shouldBatch, _replyMessages, and 61 more.

What do you think?

@chentsulin chentsulin added the doc label Jan 17, 2020
@hilezir
Copy link
Contributor Author

@hilezir hilezir commented Jan 19, 2020

I am now using simple interfaces.

export type WithGroupProps<OwnMatchProps = {}> = {
  match?: { groups?: Partial<OwnMatchProps> }
}

export type LineAction<OwnProps = {}> = (
  context: LineContext,
  props: Props<Client, LineEvent> & OwnProps,
) => Promise<Action<Client, LineEvent> | undefined | void>

// it can return props.next
expectType<LineAction>(async (context, props) => {
  console.assert(context.sendFlex.call)
  return props.next
})

// it can have OwnProps
expectType<
  LineAction<{
    ownProp1: { nestProp1: string }
    ownProp2: { nestProp2: string }
  }>
>(async (context, props) => {
  console.assert(props.ownProp1.nestProp1)
  console.assert(props.ownProp2.nestProp2)
})

// it can use with WithGroupProps
expectType<
  LineAction<
    {
      ownProp1: { nestProp1: string }
      ownProp2: { nestProp2: string }
    } & WithGroupProps<{
      command: string
    }>
  >
>(async (context, props) => {
  console.assert(props.match?.groups?.command)
  console.assert(props.ownProp1.nestProp1)
  console.assert(props.ownProp2.nestProp2)
})

FYI thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
You can’t perform that action at this time.