Skip to content

Instantly share code, notes, and snippets.

@Haosvit
Created September 26, 2022 08:47
Show Gist options
  • Save Haosvit/261755960a3b338e43f88ffdebff4707 to your computer and use it in GitHub Desktop.
Save Haosvit/261755960a3b338e43f88ffdebff4707 to your computer and use it in GitHub Desktop.
Another typescript 4 breaking change
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