Home > Net >  is it possible to check if the sentence include a certain word using keyword type in typescript?
is it possible to check if the sentence include a certain word using keyword type in typescript?

Time:12-18

I have a question about type keyword in typescript.
you can see the below code.
the argument has to include the word ('api') in the prefix.
of course, I can check it using coding such as indexOf.
but I want to use the type keyword only.

interface SomethingParam {
  url: string
}
function check(url: SomethingParam) {
  console.log(url);
}

check({ url: 'foobar' }) // error
check({ url: 'api/foobar' }) // ok

CodePudding user response:

you can try like it

function check(url: `api/${string}`) {
  console.log(url);
}

check('foobar') // error
check('api/foobar') // ok
  • Related