Joonseo Lee
Joonseo Lee

Reputation: 486

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

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

Upvotes: 2

Views: 52

Answers (1)

math-chen
math-chen

Reputation: 256

you can try like it

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

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

Upvotes: 3

Related Questions