2

I have created a new react project using the command npx create-react-app my-app --template typescript. Then introduced some type errors but yarn build is not catching those.

The changes i made are

  1. Create a new file test.tsx under the src directory with following contents
type Props = {
  message: string
}

export function getValue(props: Props): string {
  return props.message1
}
  1. Call the above function from index.tsx
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import {getValue} from "./test"

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

reportWebVitals();

console.log(getValue({message: "test"}))
  1. Run yarn build, its not throwing any typescript errors

Github repo - https://github.com/kanagarajkm/my-app-ts

Any help here is really appreciated. Thanks.

2

1 Answer 1

6

Create-react-app's typescript template uses typescript for typecheking only. You can see typescript errors in your IDE (if it supports typescript language server) while developing application. Or running tsc directly from command line.

build script uses babel-loader with preset-typescript for transpiling only. It means if typescript code is syntactically correct (but can fail type checking) it will be transpiled into .js files regardles of any typescript errors.

If you want to build your bundle only after it successfully typechecks you may modify package.json build command for react-scripts build to run after tsc finished without errors:

...
    "scripts": {
        ...
        "build": "tsc && react-scripts build",
        ...
    },
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.