question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API: incorrect declarations in types.ts

See original GitHub issue

The --strictNullChecks PR changed some declarations in types.ts to avoid frequent assertions in the compiler’s code. Unfortunately these changes make it really unsafe for API users because they no longer contain the actually nullable types: https://github.com/Microsoft/TypeScript/pull/22088#discussion_r184149465 https://github.com/Microsoft/TypeScript/pull/22088#discussion_r184155027

  • ts.Node#parent is no longer optional (which is definitely wrong for SourceFile)
  • ts.Symbol#declarations and ts.Symbol.valueDeclaration are no longer optional, but can still be undefined
  • ts.Type#symbol is no longer optional, but I guess this comment is still up to date: // Symbol associated with type (if any)

These changes should either be reverted or replaced in the published declaration files. /cc @andy-ms @weswigham

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:12
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
bradzachercommented, May 7, 2019

We just ran into this bug in @typescript-eslint. One of our rules assumed the typing was correct, causing a bug for an end user. As an API consumer, we rely on the types to help us ensure we catch bugs at compile time, and it’s counter-intuitive to handle cases that are contrary to the types.

Related issues: https://github.com/typescript-eslint/typescript-eslint/issues/498 https://github.com/typescript-eslint/typescript-eslint/issues/496

1reaction
mike-northcommented, Jan 17, 2019

repro case for ts.Symbol.valueDeclaration being undefined

// src/index.ts
export type NumberDict = { [k: string]: number };
// analyze.js
const ts = require('typescript');

const program = ts.createProgram(['src/index.ts'], {
  target: ts.ScriptTarget.ES2015,
  noEmit: true,
  strict: true
});

const checker = program.getTypeChecker();
// get the file
const [indexFile] = program.getSourceFiles().filter(f => !f.isDeclarationFile);

// file's symbol
const indexFileSymbol = checker.getSymbolAtLocation(indexFile);

// file's exports
const { exports: exportSymbols } = indexFileSymbol;

// first exported symbol from the file
const firstExport = exportSymbols.values().next().value;

console.log(
  'First symbol has type of',
  checker.typeToString(checker.getDeclaredTypeOfSymbol(firstExport)),
  'and it has a valueDeclaration of',
  firstExport.valueDeclaration
);

Result: of node analyze.js

First symbol has type of NumberDict and it has a valueDeclaration of undefined
Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Type Declarations - TypeScript
TypeScript includes declaration files for all of the standardized built-in APIs available in JavaScript runtimes. This includes things like ...
Read more >
Error: 'types' can only be used in a .ts file - Visual Studio Code ...
I'm using flow with vscode but had the same problem. I solved it with these steps: Install the extension Flow Language Support.
Read more >
TypeScript errors and how to fix them
error TS2352: Conversion of type ' { name: string; age: number; } ' to type ' Person ' may be a mistake because...
Read more >
ts-node - npm
See Missing types for details. Default: false. Environment: TS_NODE_FILES. ignoreDiagnostics. ts-node -D <code,code> ...
Read more >
Incorrect error: "TS2307: Cannot find module or its ... - YouTrack
Incorrect error: "TS2307: Cannot find module or its corresponding type declarations" from TS service.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found