question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript error "Type 'string' is not assignable to type 'AudioEncoding'"

See original GitHub issue

Environment details

  • OS: OS X Catalina 10.15.2
  • Node.js version: v13.5.0
  • Typescript version: 3.7.2
  • npm version: 6.13.4
  • @google-cloud/text-to-speech version: 2.1.0

Steps to reproduce

I’m was using the demo code, which worked fine until I updated the library from 0.5^ to 2.1.^

import {TextToSpeechClient} from  '@google-cloud/text-to-speech'

  const client = new TextToSpeechClient({
    projectId: 'PROJECT_ID',
    keyFilename: googleKey,
  });

  // Construct the request
  const request = {
    input: {
      text: text
    },
    // Select the language and SSML Voice Gender (optional)
    voice: {
      languageCode: 'fr-FR',
      name: "fr-FR-Wavenet-A",
      ssmlGender: "NEUTRAL",
    },
    // Select the type of audio encoding
    audioConfig: {
      audioEncoding: 'LINEAR16',
      sampleRateHertz: 8000,
    },
  };

const response = await client.synthesizeSpeech(request);

But now with this code, Typescript is complaining :

Argument of type ‘{ input: { text: string; }; voice: { languageCode: string; name: string; }; audioConfig: { audioEncoding: string; sampleRateHertz: number; }; }’ is not assignable to parameter of type ‘ISynthesizeSpeechRequest’. The types of ‘audioConfig.audioEncoding’ are incompatible between these types.

Argument of type ‘{ input: { text: string; }; voice: { languageCode: string; name: string; ssmlGender: string; }; audioConfig: { sampleRateHertz: number; }; }’ is not assignable to parameter of type ‘ISynthesizeSpeechRequest’. The types of ‘voice.ssmlGender’ are incompatible between these types. Type ‘string’ is not assignable to type ‘SsmlVoiceGender’.

I got it working with this :

import * as protos from "@google-cloud/text-to-speech/build/protos/protos"

const request = {
    input: {
      text: text
    },
    // Select the language and SSML Voice Gender (optional)
    voice: {
      languageCode: 'fr-FR',
      name: "fr-FR-Wavenet-A",
      ssmlGender: protos.google.cloud.texttospeech.v1.SsmlVoiceGender.NEUTRAL,
    },
    // Select the type of audio encoding
    audioConfig: {
      audioEncoding: protos.google.cloud.texttospeech.v1.AudioEncoding.LINEAR16,
      sampleRateHertz: 8000,
    },
  }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
meowlanguagescommented, Nov 30, 2020

If somebody has any similar problems, below I posted some workaround, what I found on https://westa.io/@alclimb/fp9o

THANKS TO AUTHOR, YOU SAVED MY LIFE

import dotenv from "dotenv"
import * as fs from "fs-extra"
import { TextToSpeechClient, protos } from "@google-cloud/text-to-speech"

dotenv.config({ path: `./.secrets/.env` })
main()

async function main() {
    const client = new TextToSpeechClient()
    const request: **protos.google.cloud.texttospeech.v1.ISynthesizeSpeechReques**t = {
        input: { text: `hello, world!` },

        voice: { languageCode: `en-US`, name: `en-US-Wavenet-D` },
        audioConfig: {
            audioEncoding: `MP3`,
            pitch: 0.00,
            speakingRate: 1.00
        },
    }
    const [response] = await client.synthesizeSpeech(request)
    fs.writeFileSync(`output.mp3`, response.audioContent, `binary`)

    console.log(`Audio content written to file: output.mp3`)
}
1reaction
alexander-fenstercommented, Feb 27, 2020

This fix was released, enums now accept strings.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript error "Type 'string' is not assignable to type ... - GitHub
Typescript doesn't want to compile this because the string value isn't considered the same type as the enum value, even though the strings...
Read more >
Type 'string' is not assignable to type in TypeScript | bobbyhadz
The "Type 'string' is not assignable to type" TypeScript error occurs when we try to assign a value of type `string` to something...
Read more >
Typescript Type 'string' is not assignable to type" error
I think the function returns a string and not a number as expected. export const roundWithPrecision = ( value: number, decimalPrecision: ...
Read more >
Type 'string' is not assignable to type 'string[]' : r/typescript
When I call the 'IndividualModuleContent' component and assign the 'listTextContent' property, I receive the error "Type 'string' is not ...
Read more >
Speech-to-Text streaming demo in React - createIT
Transcribing live streamed audio to text has become more and more ... for audio encoding; Used NPM libraries; Typescript; AWS Transcribe ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found