Skip to content

Latest commit

 

History

History
85 lines (68 loc) · 1.49 KB

AddTypeScriptTypes.md

File metadata and controls

85 lines (68 loc) · 1.49 KB

Add TypeScript Types

Replace $TSFixMe or any types with an inferred type. This should primarily be used to fix up TypeScript type annotations auto-generated during migration.

language js

or {
    ClassMethod(params=$params),
    FunctionDeclaration(params=$params)
} where {
    $params <: contains bubble {
        or {
            Identifier(name="$TSFixMe") => $type,
            TSAnyKeyword() as $any => $type,
            Identifier(typeAnnotation=null => $type)
        } where {
            $type = guess(codePrefix="// fix TypeScript type declarations", fallback="any", stop=["function"])
        }
    }
}

Simple Function Parameters

function getKey(userId: $TSFixMe) {
  return `some-key-${userId}`;
}

function somethingElse(num: any) {
  console.log(1 + num);
}
function getKey(userId: string) {
  return `some-key-${userId}`;
}

function somethingElse(num: number) {
  console.log(1 + num);
}

Class Definitions

class Foo {
  message = "";

  constructor(foo: any) {
    this.bar = 1;
    this.message = foo;
  }
}
class Foo {
  message = "";

  constructor(foo: string) {
    this.bar = 1;
    this.message = foo;
  }
}

Function with no types

function getKey(userId) {
  return `some-key-${userId}`;
}
function getKey(userId: string) {
  return `some-key-${userId}`;
}