0
interface o {
  name: string
}
const func = (obj: o): boolean => true

// this should throw error message.(or warning message at least, but it doesn't)
func({ name })

name is undefined in the code I wrote, so func({ name }) should throw an error I think. Is this intended?
Can I fix this with eslint or tsc config?


edit: this isn't a duplicate
I'm in node and Global.name is undefined.
It seems tsc thinks name is string though.

my tsconfig.json:

{                                                                                                                                                                               
  "compilerOptions": {                                                                                                                                                          
     "experimentalDecorators": true,                                                                                                                                             
     "emitDecoratorMetadata": true,                                                                                                                                              
     "skipLibCheck" : true,                                                                                                                                                      
     "rootDir": "./src",                                                                                                                                                         
     "outDir": "./src/js"                                                                                                                                                        
   }                                                                                                                                                                             
}
soonoo
  • 867
  • 1
  • 10
  • 35
  • Doesn't it come from global object – zerkms Sep 07 '19 at 09:19
  • Not really a duplicate IMO. This is about how TypeScript behaves in this instance. @soonoo Take a look at [this github issue](https://github.com/microsoft/TypeScript/issues/18433) – lukasgeiter Sep 07 '19 at 09:27
  • @lukasgeiter It says 'do not use `name`'. What a ridiculous solution. HAHA. – soonoo Sep 07 '19 at 09:39
  • 1
    At least there's this the [`no-restricted-globals`](https://eslint.org/docs/rules/no-restricted-globals) ESLint rule which you should be able to use for this. – lukasgeiter Sep 07 '19 at 09:42
  • Do you mean that it should cause a *compile error*? If it doesn't, does it at least throw a *runtime error*? If not, what is the value when you do `console.log(name, typeof name)`? – Bergi Sep 07 '19 at 14:19
  • You need to show the .tsconfig settings. It throws a compile error for me when I don't tell it to assume a browser environment. – JJJ Sep 07 '19 at 14:24
  • 1
    @Bergi tsc compiles above code without error/warning and compiled js code throws `ReferenceError` since `name` isn't in scope. – soonoo Sep 07 '19 at 14:52

1 Answers1

1

TypeScript declares a global called name in lib.dom.d.ts because of window.name.

name is declared with type never but that doesn't prevent it from being an issue. There is a discussion on Github on changing it's type to void in which case there would be an error in your example. A workaround is to configure ESLint with the rule no-restricted-globals.


However, since you're compiling for Node.js, there is actually a much better solution. Because you don't need any type definitions for DOM APIs it's best to not include lib.dom.d.ts at all.

You can do this by specifying the lib compiler option explicitly in tsconfig.json:

{
    "compilerOptions": {
        "lib": [
            "ES5"
        ]
    }
}

Note: the exact value for lib you'll need depends on your project. See the compiler options documentation for all available values and the defaults for different targets

lukasgeiter
  • 147,337
  • 26
  • 332
  • 270