Skip to content

Commit

Permalink
Change @type to @tsassert to avoid clashes with other tools
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeSidSmith committed Mar 8, 2020
1 parent 118e5f9 commit 0b58720
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@ You must be using TypeScript 3 (this is a peer dependency).
Simply add a comment with the following structure to the end of the line, or on the line above:

```ts
// @type: ExpectedTypeHere
// @tsassert: ExpectedTypeHere
```

Basic examples:

```ts
// Assert variable types
const myNumber = 1; // @type: number
const myNumber = 1; // @tsassert: number

// @type: number
// @tsassert: number
const myOtherNumber = 2;

// Assert return type of function
sendMessage('Hello'); // @type: Promise<string>
sendMessage('Hello'); // @tsassert: Promise<string>

// @type: Promise<string>
// @tsassert: Promise<string>
sendMessage('Hello again');

// Assert type of class instance
new MyClass(abc); // @type: MyClass<ABC>
new MyClass(abc); // @tsassert: MyClass<ABC>

// @type: MyClass<ABC>
// @tsassert: MyClass<ABC>
new MyClass(abc);
```

Expand All @@ -51,7 +51,7 @@ Example in tests:
```ts
describe('my getter', () => {
it('should return undefined if any values in the path are nullable', () => {
// @type: string | undefined
// @tsassert: string | undefined
const result = get(obj, ['a', 'b', 'c']);

expect(result).toBe(undefined);
Expand Down
16 changes: 8 additions & 8 deletions assertions/fail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ interface ABC {
d: ['a', 'b', 'c'];
}

export const abc = {} as ABC | null; // @type: ABC
export const abc = {} as ABC | null; // @tsassert: ABC

export const result = removeNull(abc); // @type: ABC | null
export const result = removeNull(abc); // @tsassert: ABC | null

removeNull(abc); // @type: ABC | null
removeNull(abc); // @tsassert: ABC | null

export const c = abc?.a?.b.c; // @type string | number
export const c = abc?.a?.b.c; // @tsassert string | number

export const d = abc?.d; // @type ['a', 'b', 'c'] | undefined
export const d = abc?.d; // @tsassert ['a', 'b', 'c'] | undefined

class MyClass<T> {
public input: T;
Expand All @@ -30,7 +30,7 @@ class MyClass<T> {
}

// tslint:disable-next-line:no-unused-expression
new MyClass(abc); // @type: MyClass<CBA | null>
new MyClass(abc); // @tsassert: MyClass<CBA | null>

// @type: ABC
removeNull(abc); // @type: ABC
// @tsassert: ABC
removeNull(abc); // @tsassert: ABC
14 changes: 7 additions & 7 deletions assertions/pass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ interface ABC {
d: ['a', 'b', 'c'];
}

export const abc = {} as ABC | null; // @type: ABC | null
export const abc = {} as ABC | null; // @tsassert: ABC | null

export const result = removeNull(abc); // @type: ABC
export const result = removeNull(abc); // @tsassert: ABC

removeNull(abc); // @type: ABC
removeNull(abc); // @tsassert: ABC

export const c = abc?.a?.b.c; // @type string | number | undefined
export const c = abc?.a?.b.c; // @tsassert string | number | undefined

export const d = abc?.d; // @type ["a", "b", "c"] | undefined
export const d = abc?.d; // @tsassert ["a", "b", "c"] | undefined

class MyClass<T> {
public input: T;
Expand All @@ -30,7 +30,7 @@ class MyClass<T> {
}

// tslint:disable-next-line:no-unused-expression
new MyClass(abc); // @type: MyClass<ABC | null>
new MyClass(abc); // @tsassert: MyClass<ABC | null>

// @type: ABC
// @tsassert: ABC
removeNull(abc);
4 changes: 2 additions & 2 deletions src/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { indent, isTruthyString } from './utils';
import { version } from './version';

const MATCHES_GLOB = /(?:}|\)|\*+\/?|\.[t]sx?)$/;
const MATCHES_LONELY_COMMENT = /^\s*?\/\/\s?@type(?::|\s)\s*(.+?)\s*?$/;
const MATCHES_TRAILING_COMMENT = /\/\/\s?@type(?::|\s)\s*(.+?)\s*?$/;
const MATCHES_LONELY_COMMENT = /^\s*?\/\/\s?@tsassert(?::|\s)\s*(.+?)\s*?$/;
const MATCHES_TRAILING_COMMENT = /\/\/\s?@tsassert(?::|\s)\s*(.+?)\s*?$/;
const MATCHES_NODE_MODULES = /^node_modules/;

interface AssertionError {
Expand Down

0 comments on commit 0b58720

Please sign in to comment.