Issue
VSCode recently updated to TypeScript 4.8, and since then I've noticed an error in one of my React projects that wasn't there before. What's more, it does not prevent the code from compiling. No error is emitted when building the project. It's difficult to summarize the actual issue, so I'll go ahead and provide some code.
First, some utility types:
interface Baz {
foo: { a: number };
}
const createDefaultExample = <K extends keyof Baz>(x: K): Baz[K] & { x: K; } => {
return { a: 0, x }; // okay in TS4.7, error in TS4.8
};
The function produces an error in 4.8, but not in 4.7:
Type '{ a: number; x: K; }' is not assignable to type 'Baz[K] & { x: K; }'.
Type '{ a: number; x: K; }' is not assignable to type 'Baz[K]'.
Type '{ a: number; x: K; }' is not assignable to type '{ a: number; }'.
Object literal may only specify known properties, and 'x' does not exist in type '{ a: number; }'.
From the error message, I can deduce that the issue comes from expanding the return type of the function. But I'm confused because it seems to do so incorrectly? The second line of the error message seems incorrect, as I wouldn't expect that assignment check to be valid for the return type.
I mentioned TypeScript 4.8 in my opening paragraph, and that is because the only change is that my VSCode also updated today, and one of the changes was to incorporate TypeScript 4.8.2.
This particular function has existed in the code for a few weeks now, and as I said, this has never shown as an error in my code before. And again, yarn build
produces no errors and compiles the code completely fine.
Solution
This was a bug in Typescript with the 4.8.2 release. It has since been fixed in the 4.8.3 release.
Answered By - Matthew Bryant
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.