Coder Social home page Coder Social logo

user-agent-data-types's Introduction

User Agent Data Types

npm

Type definition for navigator.userAgentData

Install

$ npm i -D user-agent-data-types

Usage

Make types visible in specific files

Add a TypesScript triple-slash directive as follows in any code-containing '.ts' file you want these types to be available in:

// Add data types to window.navigator for use in this file. See https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types- for more info.
/// <reference types="user-agent-data-types" />

console.log(window.navigator.userAgentData) // no type error!

Make types visible globally in all source files within a project

Create a .d.ts file anywhere in your project so that it is visible to TypeScript according to your tsconfig.json settings. For example, it could be at src/global.d.ts or src/user-agent-data-types.d.ts.

Add a TypesScript triple-slash directive as follows:

// Add data types to window.navigator ambiently for implicit use in the entire project. See https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types- for more info.
/// <reference types="user-agent-data-types" />

This exposes the types ambiently so they are available without any import or require statements. TypeScript will simply know about them everywhere.

Important: do not add any import or export statements to this file, or it will stop working ambiently. Doing that changes it in TypeScript's view from a "script" to a "module", and the rules about ambient types change in that case.

License

This project is licensed under the MIT License.

user-agent-data-types's People

Contributors

eeckhardt-chwy avatar faisalman avatar lukewarlow avatar radiofreejohn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

user-agent-data-types's Issues

Recommended change to the usage docs

Your npm package docs say to do this in tsconfig.json:

{
  "compilerOptions": {
    "types": [
        "./node_modules/user-agent-data-types"
    ]
  }
}

That is against best practice and a bit unfriendly, because as soon the types or typeRoots keys are present in tsconfig.json, that excludes all other type usage in the entire project.

The friendly way to expose ambient types from another project involves:

  1. Wrap your index.d.ts content in a declare global { } block. // this isn't necessary, actually
  2. Have your consumers create a file with a .d.ts extension in their own project, inside of their source code covered by their tsconfig.json, for example src/user-agent-data-types.d.ts:
    /// <reference types="user-agent-data-types" />

Boom. Ambient types available in consumer projects, no excluding all other types from their entire type resolution pool.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.