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

Typescript: incorrect return type for getSlot #701

Open
arnaudcourtecuisse opened this issue Jan 25, 2022 · 1 comment
Open

Typescript: incorrect return type for getSlot #701

arnaudcourtecuisse opened this issue Jan 25, 2022 · 1 comment

Comments

@arnaudcourtecuisse
Copy link

arnaudcourtecuisse commented Jan 25, 2022

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Other... Please describe:

Expected Behavior

I expect the returned value of getSlot to matche its typescript definition.

Current Behavior

The function definition declares Slot as a return type. The actual implementation of getSlot allows reutrn types null (if there are no slots) or undefined (if there are slot but not the one passed as argument).

The following code:

import { getSlot } from 'ask-sdk-core'
import type { RequestEnvelope } from 'ask-sdk-model'

export const getResolvedSlotValue = (requestEnvelope: RequestEnvelope, slotName: string) => {
  const slot = getSlot(requestEnvelope, slotName)
  if (slot.resolutions?.resolutionsPerAuthority) {
    const resolution = slot.resolutions.resolutionsPerAuthority.find(r => r.status.code === 'ER_SUCCESS_MATCH')
    if (resolution) {
      return resolution.values[0].value
    }
  }
  return null
}

.... causes the following error when the slotName is not present in the request:

TypeError: Cannot read property 'resolutions' of undefined
    at Object.getResolvedSlotValue (/var/task/src/helper/request.ts:6:12)
    ... next lines not relevant

Possible Solution

See #702

Steps to Reproduce (for bugs)

  1. Declare an intent with 2 slots (e.g. slotOne and slotTwo), and an utterance that fills only one (e.g. slotOne)
  2. Use above function in your skill handler to extract the other utterance (e.g. slotTwo)
  3. Invoke the skill intent with your utterance

Context

This helper extracts slot values from custom slot types, and then extract the ID from the value. I often rely on typing to failsafe my code. Hence, some errors can slip through when the types do not match the implementation.

Your Environment

  • ask-sdk-core@2.11.0
  • AWS Lambda Runtime Node.js 14.x

Node.js and NPM Info

  • Node.js version used for development: 14.18.1
  • NPM version used for development: 8.1.0
@rahulawl
Copy link
Contributor

Is this issue/feature-request still relevant?
We are working on prioritization of relevant issues and cleanup of rest. If we don’t hear back in 2 weeks, we will assume that the issue is not relevant and we will close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants