title: Typescript allows to save wrong types. why?
body: <p>I'm using Typescript in NodeJS. I defined an <code>interface</code> and I passed it to the variable. But when I send some data that is not match with <code>interface</code> type for example I pass <code>number</code> instead of <code>string</code> for <code>text property</code> that I created in <code>interface</code>, Typescirpt still can save them in the variable. In <code>react.js</code> I had the same issue.</p>
  <p>Code is very simple, and I'm using the memory of nodejs to make that typescript works fine:</p>
  <p><strong>models-types</strong> file:</p>
  <pre><code>export interface Todo {
    id: string;
    text: string;
  }
  </code></pre>
  <p>Controller file:</p>
  <pre><code>import { RequestHandler } from &quot;express&quot;;
  
  import { Todo } from &quot;../models/todo&quot;;
  
  const data: Todo[] = [];
  
  export const todoPostHandler: RequestHandler = (req, res, next) =&gt; {
    try {
      const text = (req.body as { text: string }).text;
  
      if (!text) {
        throw { message: &quot;invalid text&quot; };
      }
  
      const incomingData = { id: Math.random().toString(), text };
  
      data.push(incomingData);
  
      res.status(201).json({ message: &quot;data created in the server&quot;, data: incomingData });
    } catch (error) {
      return next(error);
    }
  };
  </code></pre>
  <p>Json file:</p>
  <pre><code>{
    &quot;compilerOptions&quot;: {
      /* Visit https://aka.ms/tsconfig.json to read more about this file */
  
      /* Basic Options */
      // &quot;incremental&quot;: true,                   /* Enable incremental compilation */
      &quot;target&quot;: &quot;es2018&quot; /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
      &quot;module&quot;: &quot;commonjs&quot;,
      &quot;moduleResolution&quot;: &quot;node&quot; /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
      // &quot;lib&quot;: [],                             /* Specify library files to be included in the compilation. */
      // &quot;allowJs&quot;: true,                       /* Allow javascript files to be compiled. */
      // &quot;checkJs&quot;: true,                       /* Report errors in .js files. */
      // &quot;jsx&quot;: &quot;preserve&quot;,                     /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
      // &quot;declaration&quot;: true,                   /* Generates corresponding '.d.ts' file. */
      // &quot;declarationMap&quot;: true,                /* Generates a sourcemap for each corresponding '.d.ts' file. */
      // &quot;sourceMap&quot;: true,                     /* Generates corresponding '.map' file. */
      // &quot;outFile&quot;: &quot;./&quot;,                       /* Concatenate and emit output to single file. */
      &quot;outDir&quot;: &quot;./dist&quot; /* Redirect output structure to the directory. */,
      &quot;rootDir&quot;: &quot;./src&quot; /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
      // &quot;composite&quot;: true,                     /* Enable project compilation */
      // &quot;tsBuildInfoFile&quot;: &quot;./&quot;,               /* Specify file to store incremental compilation information */
      // &quot;removeComments&quot;: true,                /* Do not emit comments to output. */
      // &quot;noEmit&quot;: true,                        /* Do not emit outputs. */
      // &quot;importHelpers&quot;: true,                 /* Import emit helpers from 'tslib'. */
      // &quot;downlevelIteration&quot;: true,            /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
      // &quot;isolatedModules&quot;: true,               /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
  
      /* Strict Type-Checking Options */
      &quot;strict&quot;: true /* Enable all strict type-checking options. */,
      // &quot;noImplicitAny&quot;: true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
      // &quot;strictNullChecks&quot;: true,              /* Enable strict null checks. */
      // &quot;strictFunctionTypes&quot;: true,           /* Enable strict checking of function types. */
      // &quot;strictBindCallApply&quot;: true,           /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
      // &quot;strictPropertyInitialization&quot;: true,  /* Enable strict checking of property initialization in classes. */
      // &quot;noImplicitThis&quot;: true,                /* Raise error on 'this' expressions with an implied 'any' type. */
      // &quot;alwaysStrict&quot;: true,                  /* Parse in strict mode and emit &quot;use strict&quot; for each source file. */
  
      /* Additional Checks */
      // &quot;noUnusedLocals&quot;: true,                /* Report errors on unused locals. */
      // &quot;noUnusedParameters&quot;: true,            /* Report errors on unused parameters. */
      // &quot;noImplicitReturns&quot;: true,             /* Report error when not all code paths in function return a value. */
      // &quot;noFallthroughCasesInSwitch&quot;: true,    /* Report errors for fallthrough cases in switch statement. */
      // &quot;noUncheckedIndexedAccess&quot;: true,      /* Include 'undefined' in index signature results */
  
      /* Module Resolution Options */
      // &quot;moduleResolution&quot;: &quot;node&quot;,            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
      // &quot;baseUrl&quot;: &quot;./&quot;,                       /* Base directory to resolve non-absolute module names. */
      // &quot;paths&quot;: {},                           /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
      // &quot;rootDirs&quot;: [],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
      // &quot;typeRoots&quot;: [],                       /* List of folders to include type definitions from. */
      // &quot;types&quot;: [],                           /* Type declaration files to be included in compilation. */
      // &quot;allowSyntheticDefaultImports&quot;: true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
      &quot;esModuleInterop&quot;: true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
      // &quot;preserveSymlinks&quot;: true,              /* Do not resolve the real path of symlinks. */
      // &quot;allowUmdGlobalAccess&quot;: true,          /* Allow accessing UMD globals from modules. */
  
      /* Source Map Options */
      // &quot;sourceRoot&quot;: &quot;&quot;,                      /* Specify the location where debugger should locate TypeScript files instead of source locations. */
      // &quot;mapRoot&quot;: &quot;&quot;,                         /* Specify the location where debugger should locate map files instead of generated locations. */
      // &quot;inlineSourceMap&quot;: true,               /* Emit a single file with source maps instead of having a separate file. */
      // &quot;inlineSources&quot;: true,                 /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
  
      /* Experimental Options */
      // &quot;experimentalDecorators&quot;: true,        /* Enables experimental support for ES7 decorators. */
      // &quot;emitDecoratorMetadata&quot;: true,         /* Enables experimental support for emitting type metadata for decorators. */
  
      /* Advanced Options */
      &quot;skipLibCheck&quot;: true /* Skip type checking of declaration files. */,
      &quot;forceConsistentCasingInFileNames&quot;: true /* Disallow inconsistently-cased references to the same file. */
    }
  }
  </code></pre>