Skip to content

Commit f24a7bd

Browse files
committed
init commit
0 parents  commit f24a7bd

File tree

9 files changed

+355
-0
lines changed

9 files changed

+355
-0
lines changed

.gitignore

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Deploy dirs
2+
lib
3+
npm
4+
5+
# Logs
6+
logs
7+
*.log
8+
npm-debug.log*
9+
yarn-debug.log*
10+
yarn-error.log*
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
24+
# nyc test coverage
25+
.nyc_output
26+
27+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
28+
.grunt
29+
30+
# Bower dependency directory (https://bower.io/)
31+
bower_components
32+
33+
# node-waf configuration
34+
.lock-wscript
35+
36+
# Compiled binary addons (https://nodejs.org/api/addons.html)
37+
build/Release
38+
39+
# Dependency directories
40+
node_modules/
41+
jspm_packages/
42+
43+
# Typescript v1 declaration files
44+
typings/
45+
46+
# Optional npm cache directory
47+
.npm
48+
49+
# Optional eslint cache
50+
.eslintcache
51+
52+
# Optional REPL history
53+
.node_repl_history
54+
55+
# Output of 'npm pack'
56+
*.tgz
57+
58+
# Yarn Integrity file
59+
.yarn-integrity
60+
61+
# dotenv environment variables file
62+
.env
63+
64+
# next.js build output
65+
.next

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 mstn
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Typescript type for fixed size arrays
2+
3+
This package contains type definitions for arrays of fixed length in Typescript.
4+
The size of the arrays is checked **at compile time**.
5+
6+
It could be useful in a variety of cases. For example, checking if the correct number of
7+
configuration options are passed to a function.
8+
9+
At the moment, we can use only immutable arrays since arithmetic operations over numeric literals have not been implemented in Typescript, yet.
10+
Workarounds without using numeric literals (e.g. define numbers by recursion) look too hacky and impractical, but any suggestion is welcome!
11+
12+
## Bugs and issues
13+
14+
Apparently, the package works as expected for many practical cases.
15+
Howerver, I do not know if its behavior is always correct.
16+
Feel free to open an issue on github if you meet something odd.
17+
18+
## Example
19+
20+
```javascript
21+
import { FixedSizeArray } from 'fixed-size-array';
22+
23+
// define a string array of length 2
24+
let d: FixedSizeArray<2, string>;
25+
26+
d = ['a', 'b']; // ok
27+
d[0] = 'a2'; // ok
28+
d[2] = 'c2'; // type error
29+
d = ['a']; // type error
30+
d = ['a', 'b', 'c']; // type error
31+
d = ['a', true]; // type error
32+
33+
d.push('d'); // type error
34+
35+
// define an empty array of strings
36+
let e: FixedSizeArray<0, string>;
37+
// we get an error, but it is not what we want
38+
// however, in this case void could be a better type
39+
// why do we want to define an immutable array of zero elements?
40+
e = []; // type error
41+
e = [] as string[]; // type error
42+
43+
// with objects does not work, as wanted
44+
let o: FixedSizeArray<2, string>;
45+
o = {
46+
length: 2,
47+
0: 'a',
48+
1: 'b'
49+
// type error
50+
// missing array methods
51+
}
52+
53+
o = {
54+
length: 2,
55+
0: 'a',
56+
1: 'b',
57+
'l': 'c'
58+
// type error
59+
// spurious property name
60+
}
61+
62+
```
63+
64+
## Current problems and limitations
65+
66+
```javascript
67+
interface Fun<N extends number, M extends number> {
68+
(a: FixedSizeArray<N, number>): FixedSizeArray<M, number>;
69+
}
70+
let f: Fun<2,3>;
71+
72+
// type error
73+
f = function(v: [number, number]) {
74+
return [1,2,3]
75+
}
76+
77+
// ok
78+
f = function(v: FixedSizeArray<2, number>) {
79+
return [1,2,3]
80+
}
81+
82+
//ok
83+
f([1,2]);
84+
```

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "fixed-size-array",
3+
"version": "0.0.0",
4+
"description": "typescript types for fixed size arrays",
5+
"types": "./src/index.d.ts",
6+
"scripts": {
7+
"deploy": "./scripts/deploy.sh"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/mstn/fixed-size-array.git"
12+
},
13+
"keywords": [
14+
"typescript",
15+
"array"
16+
],
17+
"author": "[email protected]",
18+
"license": "MIT",
19+
"bugs": {
20+
"url": "https://github.com/mstn/fixed-size-array/issues"
21+
},
22+
"homepage": "https://github.com/mstn/fixed-size-array#readme",
23+
"devDependencies": {
24+
"typescript": "^2.7.2"
25+
}
26+
}

scripts/deploy.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
# cleanup lib directory
4+
rm -rf lib || exit 0;
5+
mkdir lib;
6+
7+
# cp *.d.ts files
8+
# avoid inclusion of test files
9+
cp ./src/*.d.ts ./lib
10+
11+
# build vanilla package.json
12+
node -e "var package = require('./package.json'); \
13+
delete package.scripts; \
14+
delete package.options; \
15+
delete package.devDependencies; \
16+
package.types = 'index.d.ts'; \
17+
var fs = require('fs'); \
18+
fs.writeFileSync('./lib/package.json', JSON.stringify(package, null, 2)); \
19+
"
20+
21+
# Copy to ./npm
22+
cp README.md lib/
23+
cp LICENSE lib/
24+
25+
# publish to npm
26+
cd lib && npm publish --access public

src/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// we use numeric literal types for length as TS 2.7 does for fixed size tuples
2+
// N is the length of the array
3+
// T is the type of array elements
4+
// M is a dummy type inizialized to '0', we need it to trick the compiler
5+
export type FixedSizeArray<N extends number, T, M extends string = '0'> = {
6+
[k in M]: any;
7+
} & { length: N } & ReadonlyArray<T>;

src/test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { FixedSizeArray } from './';
2+
3+
let d: FixedSizeArray<2, string>;
4+
5+
d = ['a', 'b']; // ok
6+
d[0] = 'a2'; // ok
7+
d[2] = 'c2'; // type error
8+
d = ['a']; // type error
9+
d = ['a', 'b', 'c']; // type error
10+
d = ['a', true]; // type error
11+
12+
d.push('d'); // type error
13+
14+
let o: FixedSizeArray<2, string>;
15+
o = {
16+
length: 2,
17+
0: 'a',
18+
1: 'b'
19+
// type error
20+
// missing array methods
21+
}
22+
23+
o = {
24+
length: 2,
25+
0: 'a',
26+
1: 'b',
27+
'l': 'c'
28+
// type error
29+
// spurious property name
30+
}
31+
32+
interface Fun<N extends number, M extends number> {
33+
(a: FixedSizeArray<N, number>): FixedSizeArray<M, number>;
34+
}
35+
let f: Fun<2,3>;
36+
37+
// type error
38+
f = function(v: [number, number]) {
39+
return [1,2,3]
40+
}
41+
42+
f = function(v: FixedSizeArray<2, number>) {
43+
return [1,2,3]
44+
}
45+
f([1,2]);
46+
47+
let g: [number, number];
48+
let h: FixedSizeArray<2, number>;
49+
50+
h = [1, 2];
51+
g = [1, 2];
52+
53+
h = g;
54+
// type error
55+
g = h;

tsconfig.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"compilerOptions": {
3+
/* Basic Options */
4+
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
5+
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6+
// "lib": [], /* Specify library files to be included in the compilation. */
7+
// "allowJs": true, /* Allow javascript files to be compiled. */
8+
// "checkJs": true, /* Report errors in .js files. */
9+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
10+
// "declaration": true, /* Generates corresponding '.d.ts' file. */
11+
// "sourceMap": true, /* Generates corresponding '.map' file. */
12+
// "outFile": "./", /* Concatenate and emit output to single file. */
13+
// "outDir": "./", /* Redirect output structure to the directory. */
14+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
15+
// "removeComments": true, /* Do not emit comments to output. */
16+
// "noEmit": true, /* Do not emit outputs. */
17+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
18+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
19+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
20+
21+
/* Strict Type-Checking Options */
22+
"strict": true, /* Enable all strict type-checking options. */
23+
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
24+
// "strictNullChecks": true, /* Enable strict null checks. */
25+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
26+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
27+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
28+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
29+
30+
/* Additional Checks */
31+
// "noUnusedLocals": true, /* Report errors on unused locals. */
32+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
33+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
34+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
35+
36+
/* Module Resolution Options */
37+
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
38+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
39+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
40+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
41+
// "typeRoots": [], /* List of folders to include type definitions from. */
42+
// "types": [], /* Type declaration files to be included in compilation. */
43+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
44+
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
45+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
46+
47+
/* Source Map Options */
48+
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
49+
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
50+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
51+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
52+
53+
/* Experimental Options */
54+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
55+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
56+
}
57+
}

0 commit comments

Comments
 (0)