Skip to content

Some code became broken in 4.7 #49338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
HanabishiRecca opened this issue Jun 1, 2022 · 6 comments Β· Fixed by #49341
Closed

Some code became broken in 4.7 #49338

HanabishiRecca opened this issue Jun 1, 2022 · 6 comments Β· Fixed by #49341
Labels
Duplicate An existing issue was already created Fix Available A PR has been opened for this issue

Comments

@HanabishiRecca
Copy link

HanabishiRecca commented Jun 1, 2022

Bug Report

πŸ”Ž Search Terms

4.7

πŸ•— Version & Regression Information

Such code became broken in 4.7. It works as expected in 4.6.

⏯ Playground Link

4.6 example

4.7 example

πŸ’» Code

type TypesMap = {
    [0]: { foo: 'bar'; };
    [1]: { a: 'b'; };
};

type P<T extends keyof TypesMap> = { t: T; } & TypesMap[T];

type TypeHandlers = {
    [T in keyof TypesMap]?: (p: P<T>) => void;
};

const typeHandlers: TypeHandlers = {
    [0]: (p) => console.log(p.foo),
    [1]: (p) => console.log(p.a),
};

const onSomeEvent = <T extends keyof TypesMap>(p: P<T>) =>
    typeHandlers[p.t]?.(p);

Also it works in 4.7 with optional (?) removed from TypeHandlers index.

πŸ™ Actual behavior

The code won't compile.

πŸ™‚ Expected behavior

The code should compile.

@fatcerberus
Copy link

type Packet<T extends keyof PacketTypes> = { op: T; } & PacketTypes[T];

op: T is not narrowed by generic for some reason.

Not sure what you mean by "narrowed by generic"? You're going to have to give more details about what's going wrong. "The code won't compile" isn't very helpful.

@HanabishiRecca
Copy link
Author

HanabishiRecca commented Jun 1, 2022

"The code won't compile" isn't very helpful.

I know. Sorry for that. But I can't figure out what exactly is wrong. Code that worked at 4.6 won't compile at 4.7 for some unclear reasons. It should work by my logic (and worked as expected prior 4.7). Compiler just throws some obscure errors. That's why I'm asking for help here.
You can clone my repo by the link provided and run tsc to see.

@HanabishiRecca
Copy link
Author

Ok, I figured out something.
Such example works fine for both 4.6 and 4.7 as expected:

type TypesMap = {
    [0]: { foo: 'bar'; };
    [1]: { a: 'b'; };
};

type P<T extends keyof TypesMap> = { t: T; } & TypesMap[T];

type TypeHandlers = {
    [T in keyof TypesMap]: (p: P<T>) => void;
};

const typeHandlers: TypeHandlers = {
    [0]: (p) => console.log(p.foo),
    [1]: (p) => console.log(p.a),
};

const onSomeEvent = <T extends keyof TypesMap>(p: P<T>) =>
    typeHandlers[p.t]?.(p);

But when I add optional (?) to TypeHandlers

type TypesMap = {
    [0]: { foo: 'bar'; };
    [1]: { a: 'b'; };
};

type P<T extends keyof TypesMap> = { t: T; } & TypesMap[T];


type TypeHandlers = {
    [T in keyof TypesMap]?: (p: P<T>) => void;
};

const typeHandlers: TypeHandlers = {
    [0]: (p) => console.log(p.foo),
    [1]: (p) => console.log(p.a),
};

const onSomeEvent = <T extends keyof TypesMap>(p: P<T>) =>
    typeHandlers[p.t]?.(p);

it works as expected at 4.6 (example), but broken at 4.7 (example).

I don't know why ? breaks the code in 4.7.

@jcalz
Copy link
Contributor

jcalz commented Jun 1, 2022

Could be the same as #49242

@HanabishiRecca
Copy link
Author

Could be the same as #49242

Yeah. Too bad the error is very obscure and I wasn't even able to properly explain it.

@ahejlsberg
Copy link
Member

This is indeed a duplicate of #49242.

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label Jun 1, 2022
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Jun 1, 2022
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 4.7.3 milestone Jun 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants