Created
September 26, 2022 08:47
-
-
Save Haosvit/261755960a3b338e43f88ffdebff4707 to your computer and use it in GitHub Desktop.
Another typescript 4 breaking change
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This type definition is sort of weird, but it works in typescript v3: | |
```ts | |
interface EncodedSomething<TProps, TEncoding extends BaseEncodingSomeWhere> extends BaseEncodedThingies<TProps> { | |
encoding: TEncoding; | |
} | |
``` | |
Then in use: | |
```ts | |
class EncodedSomethingUIComponent<TSomethingToBeEncoded extends EncodedSomething<{}, | |
TSomethingToBeEncoded["encoding"] // HERE IS THE ERROR: 'TSomethingToBeEncoded["encoding"]' does not satisfy the constraint 'BaseEncodingSomeWhere'. | |
>{ | |
// impl. | |
} | |
``` | |
Don't know why this is a breaking change, can't find any where (yet). | |
For typescript 4. I fixed the error with `infer`. | |
```ts | |
type MyEncodingType<T> = T extends EncodedSomething<any, infer TEncoding> ? TEncoding : BaseEncodingSomeWhere; | |
class EncodedSomethingUIComponent<TSomethingToBeEncoded extends EncodedSomething<{}, | |
MyEncodingType<TSomethingToBeEncoded> // THE FIX. | |
>{ | |
// impl. | |
} | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment