1

I'm used to use this type of dynamic method call in vanilla JS with @prismicio/client. But with typeScript, I get an issue :

import Prismic from '@prismicio/client'

const options = {
    predicate: 'at',
    path: 'document.type',
    value: 'page'

}

Prismic.Predicates[options.predicate](options.path, options.value);

Unfortunately the parser tells me this :

{
    "resource": "/Users/a143ji/Projects/ai-marketplace-backend/src/prismic.ts",
    "owner": "typescript",
    "code": "7053",
    "severity": 8,
    "message": "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ at(fragment: string, value: string | number | boolean | Date | (string | number | boolean | Date)[]): string; not(fragment: string, value: string | number | boolean | Date | (string | ... 2 more ... | Date)[]): string; ... 28 more ...; geopoint: { ...; }; }'.",
    "source": "ts",
    "startLineNumber": 11,
    "startColumn": 16,
    "endLineNumber": 11,
    "endColumn": 45
}
Herzuull
  • 13
  • 3

1 Answers1

1

This is because options is mutable object and TS is unsure whether options.predicate could be used as index or not.

To fix it, you should add as const prefix to your object:

import Prismic from '@prismicio/client'

const options = {
    predicate: 'at',
    path: 'document.type',
    value: 'page'

} as const

Prismic.Predicates[options.predicate](options.path, options.value); // ok

Playground