Skip to content

Instantly share code, notes, and snippets.

@pablonete
Last active January 18, 2017 05:50
Show Gist options
  • Save pablonete/377561e4762b1c5b0cc19fae164b6c94 to your computer and use it in GitHub Desktop.
Save pablonete/377561e4762b1c5b0cc19fae164b6c94 to your computer and use it in GitHub Desktop.
const sources = {
repo: {
title: "hola",
count: 3,
},
editing: {
isDirty: false,
},
}
type Sources = typeof sources;
function deepCompare<KSource extends keyof Sources, KName extends keyof Sources[KSource], T>(
key: KSource,
name: KName,
value: Sources[KSource][KName],
) {
return sources[key][name] === value;
}
// This should return a type error: "3 is not a string"
deepCompare("repo", "title", 3);
// But I'm getting "title" is not assignable to type 'never'
@vaibhav-gupt
Copy link

I am getting the correct error. Saying "Argument of type '3' is not assignable to parameter of type 'string'".
Can it be related to the version of Typescript? I am using the 2.1 version from Typescript for Visual Studio.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment