Created
November 25, 2021 12:00
-
-
Save lukeknxt/cd6c3898f6b7795ebfa13413139eccbd to your computer and use it in GitHub Desktop.
WTF TypeScript
This file contains 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
interface Service { | |
update(obj: { id?: string }): void; | |
// changing this to a function type `update: (obj: {id?: string}) => void` works | |
// and causes the implementation not to type-check | |
} | |
class ServiceImpl implements Service { | |
// somehow this is a valid implementation | |
update(obj: { id: string }): void { | |
// `id` can never be undefined or null here according to the type system | |
console.log(typeof obj.id); // definitely string surely! | |
} | |
} | |
function program(service: Service): void { | |
return service.update({}); // didn't pass an `id` | |
} | |
// prints `undefined` from an implementation where it shouldn't be possible | |
program(new ServiceImpl()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related to microsoft/TypeScript#20537