Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this TypeScript program compile?

Tags:

typescript

Let's start with an apology for the overly generic question, but I am wrecking my head how to narrow it down due to lack of understanding of what's happening here, so will adjust based on comments / answers.

I have created a sample program on TypeScript Playground that should not compile but does (using latest version of TypeScript, v4.5.2 as of this writing.) I am trying to understand why this compiles in order to understand what other bad programs might sneak past the type checker and go undetected. Here is another version of the program that shows the problem at runtime. Are there any options I have overlooked or anything I can do to have this program not type check?

For convenience, I inline the program in question here as well:

type Settings = { 'a': boolean; 'b': string }

function setSetting<K extends keyof Settings>(p: K, v: Settings[K]): void {}

// setSetting('a', 100) // good; it fails
setSetting('a', true); // good; compiles

function breakSetSetting(k: keyof Settings, v: boolean): void {
  setSetting(k, v);
}

breakSetSetting('b', true); // bad; it compiles!

Update: As per comment by @crashmstr using generics for breakSettings makes the program not compile, however, it's still unclear to me why it was allowed to compile before.

like image 963
Felix Schlitter Avatar asked Dec 03 '21 21:12

Felix Schlitter


People also ask

Why TypeScript is compiled?

The TypeScript compiler compiles these files and outputs the JavaScript with . js extension by keeping the same file name as the individual input file. The TypeScript compiler also preserves the original file path, hence the . js output file will be generated where the input file was in the directory structure.

Why does TypeScript compile to JavaScript?

TypeScript files are compiled to readable JavaScript, so that migration back is possible and understanding the compiled TypeScript is not hard at all.

Do you have to compile TypeScript everytime?

As we know, after writing any TypeScript file, we need to compile it every time and based on that, the . js file will be generated. So, every time, we should compile the TypeScript file by writing the command in the console.

How does TypeScript compiler work?

TypeScript works by adding enhanced syntax to JavaScript and then transforming it to JavaScript after the TypeScript compiler does its own checks. It doesn't change JavaScript's type system. Instead, it adds more checks to it.


Video Answer


1 Answers

When you define K, you are defining it to be 'a' | 'b' (which is keyof K). This means that v would be Settings['a' | 'b'] which evaluates to boolean | string. Now when your breakSetSetting passes a boolean to setSetting, boolean is in the scope of boolean | string, making the compiler pass.

like image 159
Saiansh Singh Avatar answered Nov 15 '22 10:11

Saiansh Singh