#Type <A> does not satisfy the expected type <T>. <A> is assignable to the constraint of type <T>,

10 messages · Page 1 of 1 (latest)

hearty frigate
drifting stratusBOT
#

@hearty frigate Here's a shortened URL of your playground link! You can remove the full link from your message.

elliotyoyo#0

Preview:```ts
type Languages = 'fr' | 'en'

function test<L extends Languages>() {

const thing = 'fr' satisfies Languages
const thing2 = 'fr' satisfies L

}```

clever seal
#

Imagine you wrote test<"en">

#

would "fr" satisfy L extends Language when L = "en"?

hearty frigate
#

Alright I get it now thx !

Do you know a way to check & narrow the type of L here ?
I Guess I would need a separate function ?

#

Using .some doesn't trigger a ts error in the if condition, but doesn't narrow the type

drifting stratusBOT
#
elliotyoyo#0

Preview:```ts
type Languages = 'fr' | 'en'

function test<L extends Languages>(allowedLanguages: L[]) {

const pickedLanguage = 'fr'

if (!allowedLanguages.includes(pickedLanguage)) {
    throw new Error('Nope')
}

if (!allowedLanguages.some((item) => item === pickedLanguage)) {

...```

prisma ingot
#

One simple way is to do:

const language = allowedLanguages.find(language => language === pickedLanguage)
#

Then check the returned language and use it from then on.

hearty frigate
#

I created a new variable with as L, but using find to narrow to type into a new variable is smarter !
Thanks