Skip to content

Instantly share code, notes, and snippets.

@przemyslawjanpietrzak
Last active December 18, 2018 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save przemyslawjanpietrzak/68f7908e591a101c0447c744278f9dff to your computer and use it in GitHub Desktop.
Save przemyslawjanpietrzak/68f7908e591a101c0447c744278f9dff to your computer and use it in GitHub Desktop.
interface Data {
fn(arg: string): Array<string>
fn(arg: number): null
}
let data: Data;
const a = data.fn(42); // null
const b = data.fn("str"); // Array<string>
interface API {
"/users": { params: [], response: User[]}
"/users/:id": { params: [number], response: User}
}
let users: API['/users'] // { params: [], response: User[]}
let user: API['/users/:id'] // params: [number], response: User
const fn = (arg: 0 | 1) => 42;
fn(0); // OK
fn(1); // OK
fn(2); // ERROR Argument of type '2' is not assignable to parameter of type '0 | 1'.
(num: number) => fn(num); // ERROR Argument of type 'number' is not assignable to parameter of type '0 | 1'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment