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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get features in typescript without casting? #1296

Open
nkint opened this issue May 21, 2022 · 2 comments
Open

How to get features in typescript without casting? #1296

nkint opened this issue May 21, 2022 · 2 comments
Labels

Comments

@nkint
Copy link

nkint commented May 21, 2022

Question:

Hi!
Thank you for the great library! I was looking for a typescript audio feature analysis for a while and.. 馃殌鈿★笍馃挜

Sorry this is my first question and it might be dumb and born out of lack of examples.

I'm in typescript, and I'd like to understand how to retrive one single feature without casting.

type State = {
  energy: null | number
}

const state: State = {
  energy: null
}

// ... down into retrieve audio features
state.energy = meydaAnalyzer.get('energy') // ERROR HERE

I get the error:

Type 'Partial<MeydaFeaturesObject>' is not assignable to type 'number'.

I'm solving it with brutal casting:

state.energy = meydaAnalyzer.get('energy') as number

I don't have any specific settings and this error seems to be quite general and linked to a too miuch flexible .get typings, but I'm sure you have a better solution.

Enviroment:

For completeness this is my setup (I don't think it is important but, you know...):

versions:

"meyda": "^5.4.0",
"typescript": "^4.6.4",
"vite": "^2.9.9",

and my tsconfig.json:

{
  "compilerOptions": {
    "target": "ESNext",
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "types": ["vite/client"],
    "baseUrl": "src",
    "allowJs": true,
    "skipLibCheck": false,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "ESNext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react",
    "downlevelIteration": true,
    "alwaysStrict": true,
    "strictNullChecks": true
  },
  "include": ["./src"],
  "exclude": ["node_modules"]
}
@nkint nkint added the question label May 21, 2022
@hughrawlinson
Copy link
Member

Hi!

I've actually been working on a PR for this - it's not ready at the moment and the complexity make me think maybe it's not the right thing to merge for maintainability reasons. I would love feedback on the design described here though!

For now, I would suggest writing a user defined type guard function.

@nkint
Copy link
Author

nkint commented May 24, 2022

Hi @hughrawlinson thanks for the answer!

I was thinking to replace .get to my own typed get function:

type FeatureName = keyof MeydaFeaturesObject
function getFeatures <KS extends FeatureName>(keys: readonly KS[]): Pick<Features, KS> {
   return meydaAnalyzer.get(keys)
}

the new extract API seems very interesting! 馃憦

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

No branches or pull requests

2 participants