Meteor typescript don't check validate data type?

Now I tried to use Meteor + Typescript:

// server/services.ts (Incorrect function to return)
export default function hello(msg: string): number {
  return msg
}
--------------------------
// server/methods.ts
import hello from './services'

Meteor.methods({
  hello(msg) {
    return hello(msg);
  }
});

------------- 
// client/...
Meteor.call("hello", "Rabbit", (err, res) => {
   console.log(res); // Work fine
});

I still work fine, Pl help me!
My tsconfig.json

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es2018",
    "module": "esNext",
    "lib": ["esnext", "dom"],
    "allowJs": true,
    "checkJs": false,
    "jsx": "preserve",
    "incremental": true,
    "noEmit": true,

    /* Strict Type-Checking Options */
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,

    /* Additional Checks */
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": false,
    "noFallthroughCasesInSwitch": false,

    /* Module Resolution Options */
    "baseUrl": ".",
    "paths": {
      /* Support absolute /imports/* with a leading '/' */
      "/*": ["*"]
    },
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "types": ["node", "mocha"],
    "esModuleInterop": true,
    "preserveSymlinks": true
  },
  "exclude": [
    "./.meteor/**",
    "./packages/**"
  ]
}

Or Typescript only check error on IDE (VSCode)???
But still work fine when running!!!

Yeah, Meteor doesn’t do any type checking, even with the typescript package.
The idea being that you can run typescript on it’s own for checking, or as a pre-commit hook and use IDE features for the rest.

VSCode has such good typescript support that I hardly notice that Meteor isn’t doing any checking

1 Like

@coagmano, thanks for your reply.
It mean that VSCode is alter all error type typescript.
But still work while we run meteor.

How to config to check debug when runing, if typescript is error???

// Example
let data: number
data = "Hello"

Would like to show error when
meteor run

I would love to have type checking in Meteor as well, are there any plans for it to be added to the typescript package?

Also, does anyone have suggestions for how to set up the pre-commit hook?

1 Like

I use husky for hooks, with lint-staged like this (in package.json):

  "husky": {
    "hooks": {
      "pre-commit": "tsc --noEmit && npm run lint-staged"
    }
  },  
  "lint-staged": {
    "*.{js,jsx,ts,tsx}": [
      "eslint",
      "prettier --write"
    ],
    "*.styl": [
      "stylus-supremacy format --options ./.stylus-supremacy.json --replace"
    ]
  },

EDIT: Realised I had a bug, so moved tsc --noEmit to the hook, so it runs with the whole project context (using tsconfig) rather than on each changed file individually

4 Likes

@coagmano, thanks for your advice.
But I am not clear about this.
Could you share any example Meteor Type Sample App?

@theara, I was specifically answering this question:

Which runs tsc --noEmit to typecheck the project before a commit

2 Likes

I have been using this precommit hook - it is great! Thanks for the tip!

Using just typescript only is not the solution. If someone hacks my client and inserts an object instead of the string it is not really save if I want to be 100 % sure that there is no wrong type in my database

That a case for Sherlock Holmes SimpleSchema, usually, or for check. Methods and subscriptions definitely need to be guarded with strong parameter type checking.

2 Likes