softshipper
softshipper

Reputation: 34071

Type 'string' is not assignable to type 'A'

I have the following code snippet that does not compile:

interface A {
    a: string
}

export type SessionSsrProps<T> = Record<string, T> & {
    signOutUrl: string
}

const x: SessionSsrProps<A> = {
    x: {
        a: "Hello"
    },
    signOutUrl: "Hello"
}

the compiler complains:

Type 'string' is not assignable to type 'A'

What am I doing wrong?

I also tried:


interface SessionSsrProps<T> {
    [key:string]: T
    signOutUrl: string
}

and the compiler complains:

Property 'signOutUrl' of type 'string' is not assignable to string index type 'T'.

Upvotes: 0

Views: 92

Answers (1)

backtick
backtick

Reputation: 2775

I can't say confidently whether what you're doing is possible or not, but it's likely that it is not.

Both your attempts fail because you're describing an impossible type: a record with zero or more keys whose values are all of type T, AND also there is a key signOutUrl with a value of type string. This requires signOutUrl to satisfy both string & T, which is unresolvable given that T does not extend string.

What you want to express is a record with zero or more keys whose values are all of type T, EXCEPT the key signOutUrl which has a value of type string. I believe (but am not certain) that this is not currently expressible in TypeScript.

Upvotes: 1

Related Questions