alexa-skills-kit-sdk-for-nodejs
alexa-skills-kit-sdk-for-nodejs copied to clipboard
Typescript: incorrect return type for getSlot
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)
- Declare an intent with 2 slots (e.g.
slotOne
andslotTwo
), and an utterance that fills only one (e.g.slotOne
) - Use above function in your skill handler to extract the other utterance (e.g.
slotTwo
) - 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
- [email protected]
- 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
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.