Skip to content

Instantly share code, notes, and snippets.

@metafeather
Last active May 5, 2017 12:38
Show Gist options
  • Save metafeather/7e2af63ae006a0ee00b2fdce6f9a0479 to your computer and use it in GitHub Desktop.
Save metafeather/7e2af63ae006a0ee00b2fdce6f9a0479 to your computer and use it in GitHub Desktop.
Examples of VSCode 1.12.1 TypeScript Types inferred from ES6 code
{
/*
* Examples of VSCode 1.12.1 TypeScript Types inferred from ES6 code.
* Conclusion: You are not required to use JSDoc or TypeScript
* annotations in most cases if you use default values in functions.
*/
let easy = 'abc';
// Error: Type '123' is not assignable to type 'string'
easy = 123;
// Can types be infered from ES6 default values rather than use JSDoc?
const arrayId = (a = []) => a;
arrayId([]);
// Error: Argument of type 'string' is not assignable to parameter
// of type 'any[]'
arrayId(easy);
// So long as the Class has an instance property it is correctly infered
// as a Class type for checking in Function arguments.
class Example {
static is() {
return 'Example';
}
get me() {
return Example.is;
}
}
const ex = new Example();
const fakeEx = {
set me(v) {
this._me = v;
},
get me() {
return this._me || 'fakeMe';
},
};
/**
* @param {Example} x
* @return {Example}
*/
const exampleId = (x) => x;
// Error: Supplied parameters do not match any signature of call target.
exampleId();
// Error: Argument of type '""' is not assignable to parameter
// of type 'Example'
exampleId('');
// Error: Argument of type 'typeof Example' is not assignable to parameter
// of type 'Example'.
// Property 'me' is missing in type 'typeof Example'.
exampleId(Example);
// Error: Argument of type '{}' is not assignable to parameter
// of type 'Example'.
// Property 'me' is missing in type '{}'.
exampleId({});
// Error: Argument of type '() => void' is not assignable to parameter
// of type 'Example'.
// Property 'me' is missing in type '() => void'.
exampleId(() => {});
// correct
exampleId(ex);
// correct
exampleId(new Example());
// correct Interface
exampleId(fakeEx);
const exampleIdClassDefault = (x = Example) => x;
// correct - uses default value
exampleIdClassDefault();
// Error: Argument of type '""' is not assignable to parameter
// of type 'Example'
exampleIdClassDefault('');
// Error: Argument of type '{}' is not assignable to parameter
// of type 'typeof Example'.
// Property 'prototype' is missing in type '{}'.
exampleIdClassDefault({});
// Error: Argument of type '() => void' is not assignable to parameter
// of type 'Example'.
// Property 'me' is missing in type '() => void'.
exampleIdClassDefault(() => {});
// correct
exampleIdClassDefault(Example);
// Error: Argument of type 'Example' is not assignable to parameter
// of type 'typeof Example'.
// Property 'prototype' is missing in type 'Example'.
exampleIdClassDefault(ex);
// Error: Argument of type 'Example' is not assignable to parameter
// of type 'typeof Example'.
exampleIdClassDefault(new Example());
// Error: Argument of type '{ [x: string]: any; me: any; }' is not
// assignable to parameter of type 'typeof Example'.
// Property 'prototype' is missing in type '{ [x: string]: any; me: any; }'.
exampleIdClassDefault(fakeEx);
const exampleIdInstanceDefault = (x = new Example()) => x;
// correct - uses default value
exampleIdInstanceDefault();
// Error: Argument of type '""' is not assignable to parameter
// of type 'Example'
exampleIdInstanceDefault('');
// Error: Argument of type '{}' is not assignable to parameter
// of type 'typeof Example'.
// Property 'prototype' is missing in type '{}'.
exampleIdInstanceDefault({});
// Error: Argument of type '() => void' is not assignable to parameter
// of type 'Example'.
// Property 'me' is missing in type '() => void'.
exampleIdInstanceDefault(() => {});
// Error: Argument of type 'typeof Example' is not assignable to parameter of type 'Example'.
exampleIdInstanceDefault(Example);
// correct
exampleIdInstanceDefault(ex);
// correct
exampleIdInstanceDefault(new Example());
// correct Interface
exampleIdInstanceDefault(fakeEx);
const exampleIdInstanceDefaultVar = (x = ex) => x;
// correct - uses default value
exampleIdInstanceDefaultVar();
// Error: Argument of type '""' is not assignable to parameter
// of type 'Example'
exampleIdInstanceDefaultVar('');
// Error: Argument of type '{}' is not assignable to parameter
// of type 'typeof Example'.
// Property 'prototype' is missing in type '{}'.
exampleIdInstanceDefaultVar({});
// Error: Argument of type '() => void' is not assignable to parameter
// of type 'Example'.
// Property 'me' is missing in type '() => void'.
exampleIdInstanceDefaultVar(() => {});
// Error: Argument of type 'typeof Example' is not assignable to parameter of type 'Example'.
exampleIdInstanceDefaultVar(Example);
// correct
exampleIdInstanceDefaultVar(ex);
// correct
exampleIdInstanceDefaultVar(new Example());
// correct Interface
exampleIdInstanceDefaultVar(fakeEx);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment